Mail Archives: djgpp-workers/2002/01/05/13:20:29
> From: "Tim Van Holder" <tim DOT van DOT holder AT pandora DOT be>
> Date: Sat, 5 Jan 2002 18:31:01 +0100
>
> > > *d = '\0';
> > > - /* Free unused space. */
> > > - cmd->command = (char *) realloc (cmd->command, cmd_len + 1);
> > > + /* Free unused space, if we can. */
> > > + d = (char *) realloc (cmd->command, cmd_len + 1);
> > > + if (d)
> > > + cmd->command = d;
> >
> > No, please don't! This code compacts the string, and thus the result
> > will always be _smaller_ than the original. So if realloc fails here,
> > the right thing to do would be to return the original string.
>
> Which is what happens - instead of losing the original pointer (which
> is what the old code did), it now puts in the pointer returned by
> realloc only if it wasn't NULL; otherwise cmd->command remains
> unchanged.
If realloc always succeeds, there's no need for the extra code. If
it can sometimes fail, we are risking losing the original command
line inside realloc.
Extra code, if it is unused, is a bad idea in this case, since
dbgredir functions are called each time control flow jumps between
GDB and the debuggee, so it needs to run as fast as possible.
- Raw text -