X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-workers-bounces using -f Date: Sat, 05 Jan 2002 20:15:55 +0200 From: "Eli Zaretskii" Sender: halo1 AT zahav DOT net DOT il To: "Tim Van Holder" Message-Id: <3069-Sat05Jan2002201554+0200-eliz@is.elta.co.il> X-Mailer: emacs 21.1.50 (via feedmail 8 I) and Blat ver 1.8.9 CC: djgpp-workers AT delorie DOT com, rich AT phekda DOT freeserve DOT co DOT uk In-reply-to: <000001c1960e$bf2c61f0$cef8e0d5@zastaixp> (tim DOT van DOT holder AT pandora DOT be) Subject: Re: Memory leaks fixes References: <000001c1960e$bf2c61f0$cef8e0d5 AT zastaixp> Reply-To: djgpp-workers AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp-workers AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk > From: "Tim Van Holder" > 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.