From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4939 invoked by alias); 20 Jul 2011 15:45:52 -0000 Received: (qmail 4931 invoked by uid 22791); 20 Jul 2011 15:45:51 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail.codesourcery.com (HELO mail.codesourcery.com) (38.113.113.100) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 20 Jul 2011 15:45:17 +0000 Received: (qmail 31616 invoked from network); 20 Jul 2011 15:45:16 -0000 Received: from unknown (HELO scottsdale.localnet) (pedro@127.0.0.2) by mail.codesourcery.com with ESMTPA; 20 Jul 2011 15:45:16 -0000 From: Pedro Alves To: Tom Tromey Subject: Re: [rfc] Prompt memory management/cleanups Date: Wed, 20 Jul 2011 16:04:00 -0000 User-Agent: KMail/1.13.6 (Linux/2.6.38-8-generic; KDE/4.6.2; x86_64; ; ) Cc: pmuldoon@redhat.com, gdb-patches@sourceware.org References: <201107201612.23708.pedro@codesourcery.com> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Message-Id: <201107201645.14136.pedro@codesourcery.com> X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-07/txt/msg00541.txt.bz2 On Wednesday 20 July 2011 16:20:57, Tom Tromey wrote: > >>>>> "Pedro" == Pedro Alves writes: > > Pedro> You've asked for comments on the API, and IMO this makes for > Pedro> a weird API, because the caller of set_prompt needs to know > Pedro> whether set_prompt will take ownership of the pointer or not > Pedro> depending on where the pointer came from. I haven't looked > Pedro> at the callers -- that's why I asked what would need to > Pedro> change. :-) > > The problem case is set_prompt(get_prompt()), but here the prompt code > already owns the pointer. I don't think that would change if you make set_prompt xstrdup before xfree. > I guess it is a little weird, but it still > falls under the general rule of "you have to call get_prompt again to > get the prompt after set_prompt". The current code allows: p = get_prompt(0); set_prompt(p, 0); // p is still valid here whereas: p = get_prompt(0); set_prompt("foo", 0); // p is invalid here But if we already have that rule, then we can treat the former as undefined. I think we should still xstrdup before xfree though as a general rule in these sort of scenarios. Otherwise: p = get_prompt(0); (p's len being > 0) set_prompt(p + 1, 0); will end up with garbage. -- Pedro Alves