public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
From: Jim Meyering <jim@meyering.net>
To: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
Cc: linux-ext4@vger.kernel.org
Subject: Re: avoid leak upon failed realloc
Date: Mon, 18 Jun 2007 16:10:44 +0200	[thread overview]
Message-ID: <87wsy15p5n.fsf@rho.meyering.net> (raw)
In-Reply-To: <1182175456.13184.7.camel@kleikamp.austin.ibm.com> (Dave Kleikamp's message of "Mon\, 18 Jun 2007 09\:04\:16 -0500")

Dave Kleikamp <shaggy@linux.vnet.ibm.com> wrote:
> Not a big deal, but this part could be simplified to:
>
> 		if (new_dptr == NULL)
> 			free(dbuf.dptr);
> 		dbuf.dptr = new_dptr;

Of course!
Thanks!

In case it helps, here's the updated patch:

Here's a tiny fix to avoid a leak when realloc fails:

2007-06-16  Jim Meyering  <jim@meyering.net>

	* tdb.c (tdb_append): Don't leak a buffer when realloc fails.

diff -r 777972a573b3 lib/ext2fs/tdb.c
--- a/lib/ext2fs/tdb.c	Fri Jun 15 18:05:09 2007 +0200
+++ b/lib/ext2fs/tdb.c	Mon Jun 18 16:08:24 2007 +0200
@@ -3460,8 +3460,12 @@ int tdb_append(struct tdb_context *tdb,
 	if (dbuf.dptr == NULL) {
 		dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
 	} else {
-		dbuf.dptr = (unsigned char *)realloc(dbuf.dptr,
-						     dbuf.dsize + new_dbuf.dsize);
+		unsigned char *new_dptr
+		  = (unsigned char *)realloc(dbuf.dptr,
+					     dbuf.dsize + new_dbuf.dsize);
+		if (new_dptr == NULL)
+			free(dbuf.dptr);
+		dbuf.dptr = new_dptr;
 	}
 
 	if (dbuf.dptr == NULL) {

Signed-off-by: Jim Meyering <jim@meyering.net>

  reply	other threads:[~2007-06-18 14:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-16 17:31 avoid leak upon failed realloc Jim Meyering
2007-06-18 14:04 ` Dave Kleikamp
2007-06-18 14:10   ` Jim Meyering [this message]
2007-06-22  6:41     ` Theodore Tso
2007-06-22 17:27       ` Jeremy Allison
2007-06-22 20:20         ` Theodore Tso
2007-06-22 21:02           ` Jeremy Allison

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wsy15p5n.fsf@rho.meyering.net \
    --to=jim@meyering.net \
    --cc=linux-ext4@vger.kernel.org \
    --cc=shaggy@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox