This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [rfc] Prompt memory management/cleanups
On Wednesday 20 July 2011 16:04:16, Phil Muldoon wrote:
> Pedro Alves <pedro@codesourcery.com> writes:
>
> > On Wednesday 20 July 2011 15:30:19, Phil Muldoon wrote:
> >> s = get_prompt (0)
> >> set_prompt (s, 0)
> >>
> >> Without that check, 'PROMPT (level)' would be freed, but 's' points to
> >> that. So you set garbage. get_prompt returns a pointer, not a copy.
> >
> > I'm probably missing something, but isn't it just
> > a matter of instead of having:
> >
> > + xfree (PROMPT (level));
> > + PROMPT (level) = xstrdup (s);
> >
> > you have:
> >
> > + char *newp = xstrdup (s);
> > + xfree (PROMPT (level));
> > + PROMPT (level) = newp;
> >
> > ?
>
> Yeah I noted we could do that in my reply. Sure we can do that, I'm not
> opposed to it. But I am not sure on your objection to the check we make
> first instead of the xstrdup? If PROMPT (level) == s, then there is no
> need to copy the contents of s into PROMPT, it is already there? The
> user is effectively asking for a noop?
You've asked for comments on the API, and IMO this makes for
a weird API, because the caller of set_prompt needs to know
whether set_prompt will take ownership of the pointer or not
depending on where the pointer came from. I haven't looked
at the callers -- that's why I asked what would need to
change. :-)
--
Pedro Alves