From owner-freebsd-hackers Thu Aug 20 22:24:34 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id WAA07784 for freebsd-hackers-outgoing; Thu, 20 Aug 1998 22:24:34 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from gatekeeper.tsc.tdk.com (gatekeeper.tsc.tdk.com [207.113.159.21]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id WAA07779 for ; Thu, 20 Aug 1998 22:24:33 -0700 (PDT) (envelope-from gdonl@tsc.tdk.com) Received: from sunrise.gv.tsc.tdk.com (root@sunrise.gv.tsc.tdk.com [192.168.241.191]) by gatekeeper.tsc.tdk.com (8.8.8/8.8.8) with ESMTP id WAA25174 for ; Thu, 20 Aug 1998 22:23:45 -0700 (PDT) (envelope-from gdonl@tsc.tdk.com) Received: from salsa.gv.tsc.tdk.com (salsa.gv.tsc.tdk.com [192.168.241.194]) by sunrise.gv.tsc.tdk.com (8.8.5/8.8.5) with ESMTP id WAA13115 for ; Thu, 20 Aug 1998 22:23:44 -0700 (PDT) Received: (from gdonl@localhost) by salsa.gv.tsc.tdk.com (8.8.5/8.8.5) id WAA08503 for hackers@FreeBSD.ORG; Thu, 20 Aug 1998 22:23:43 -0700 (PDT) From: Don Lewis Message-Id: <199808210523.WAA08503@salsa.gv.tsc.tdk.com> Date: Thu, 20 Aug 1998 22:23:43 -0700 X-Mailer: Mail User's Shell (7.2.6 alpha(3) 7/19/95) To: hackers@FreeBSD.ORG Subject: Re: Realloc fix for review Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Aug 20, 10:19am, Warner Losh wrote: } Subject: Realloc fix for review } } Recently, OpenBSD went through their source tree and fixed all } instances of } a = realloc(a, size); } with } na = realloc(a, size); } if (!na) } free(a); } a = na; } } So rather than hack some really complicated a and na's, I write a new } function, added it to libc and then modified all of src/lib to use } this new function. I called it frealloc (freeing realloc). I added } it to the man page, etc. I know that this isn't in ANSI C nor in } POSIX, but after seeing how hard it was to get people to use strncpy } right, I thought it would be good to add this to libc. I'd worry about causing name space pollution. You might break someone else's code who has their own function called frealloc ... On Aug 20, 4:48pm, Dag-Erling Coidan =?iso-8859-1?Q?Sm=F8rgrav?= wrote: } Subject: Re: Realloc fix for review } Is that really a good idea? If you free the old block when realloc() } fails, you lose whatever data was in it (and therefore potentially } lose the ability to generate a sensible error message or recover } gracefully). Such a change should be done on a per-case basis, rather } than blindly applied to every snippet that calls realloc(). The original code also loses access to the data and leaks core because it overwrites a even if realloc() failed. That is, unless you kept another copy of the pointer somewhere, but that would be bad because that pointer might be pointing to some bogus location in the heap if realloc succeeded. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message