Re: avoid leak upon failed realloc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dave Kleikamp <shaggy@xxxxxxxxxxxxxxxxxx> 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@xxxxxxxxxxxx>

	* 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@xxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Reiser Filesystem Development]     [Ceph FS]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux FS]     [Yosemite National Park]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Device Mapper]     [Linux Media]

  Powered by Linux