From: Rusty Russell Date: Mon, 29 Jun 2009 02:17:52 +0000 (+0930) Subject: Fix append of zero-length records to zero-length records. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=cd065f97d15e08493e96972c9e4b9a619cd26154 Fix append of zero-length records to zero-length records. realloc() has that horrible overloaded free semantics. --- diff --git a/ccan/tdb/tdb.c b/ccan/tdb/tdb.c index dffc55b9..db5e5b49 100644 --- a/ccan/tdb/tdb.c +++ b/ccan/tdb/tdb.c @@ -623,8 +623,13 @@ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf) if (dbuf.dptr == NULL) { dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize); } else { - unsigned char *new_dptr = (unsigned char *)realloc(dbuf.dptr, - dbuf.dsize + new_dbuf.dsize); + unsigned int new_len = dbuf.dsize + new_dbuf.dsize; + unsigned char *new_dptr; + + /* realloc '0' is special: don't do that. */ + if (new_len == 0) + new_len = 1; + new_dptr = (unsigned char *)realloc(dbuf.dptr, new_len); if (new_dptr == NULL) { free(dbuf.dptr); }