From 7a0bc5322114d792c5bce0f8f3d5f6a060670e36 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 2 Jan 2000 01:40:35 +0000 Subject: - optimise tdb_store() a little - prevent a free of an unallocated pointer (This used to be commit b35b5c63886823bb8b8d8c5c29ccc7b315a7de30) --- source3/tdb/tdb.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/source3/tdb/tdb.c b/source3/tdb/tdb.c index 8eb44d03fb..7b8f8db4c0 100644 --- a/source3/tdb/tdb.c +++ b/source3/tdb/tdb.c @@ -908,7 +908,7 @@ int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key) unsigned hash; tdb_off offset, rec_ptr, last_ptr; struct list_struct rec, lastrec; - char *data; + char *data = NULL; /* find which hash bucket it is in */ hash = tdb_hash(&key); @@ -1004,9 +1004,9 @@ int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key) int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) { struct list_struct rec; - char *data = NULL; unsigned hash; tdb_off rec_ptr, offset; + char *p = NULL; /* find which hash bucket it is in */ hash = tdb_hash(&key); @@ -1053,10 +1053,18 @@ int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) rec.full_hash = hash; rec.magic = TDB_MAGIC; - /* write the new record */ - if (rec_write(tdb, rec_ptr, &rec) == -1) goto fail; - if (tdb_write(tdb, rec_ptr + sizeof(rec), key.dptr, key.dsize) == -1) goto fail; - if (tdb_write(tdb, rec_ptr + sizeof(rec) + key.dsize, dbuf.dptr, dbuf.dsize) == -1) goto fail; + p = (char *)malloc(sizeof(rec) + key.dsize + dbuf.dsize); + if (!p) goto fail; + + memcpy(p, &rec, sizeof(rec)); + memcpy(p+sizeof(rec), key.dptr, key.dsize); + memcpy(p+sizeof(rec)+key.dsize, dbuf.dptr, dbuf.dsize); + + if (tdb_write(tdb, rec_ptr, p, sizeof(rec)+key.dsize+dbuf.dsize) == -1) + goto fail; + + free(p); + p = NULL; /* and point the top of the hash chain at it */ if (ofs_write(tdb, offset, &rec_ptr) == -1) goto fail; @@ -1068,7 +1076,7 @@ int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) #if TDB_DEBUG printf("store failed for hash 0x%08x in bucket %u\n", hash, BUCKET(hash)); #endif - if (data) free(data); + if (p) free(p); tdb_unlock(tdb, BUCKET(hash)); return -1; } -- cgit