This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [rfc] Prompt memory management/cleanups


On Wednesday 20 July 2011 16:20:57, Tom Tromey wrote:
> >>>>> "Pedro" == Pedro Alves <pedro@codesourcery.com> 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]