Fix append of zero-length records to zero-length records.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 29 Jun 2009 02:17:52 +0000 (11:47 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 29 Jun 2009 02:17:52 +0000 (11:47 +0930)
realloc() has that horrible overloaded free semantics.

ccan/tdb/tdb.c

index dffc55b94d6fb1e16ed728fc267059d2c05e35a2..db5e5b49e4b663eeb290401d7346342d870b6a28 100644 (file)
@@ -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);
                }