summaryrefslogtreecommitdiff
path: root/source3
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2000-01-02 01:40:35 +0000
committerAndrew Tridgell <tridge@samba.org>2000-01-02 01:40:35 +0000
commit7a0bc5322114d792c5bce0f8f3d5f6a060670e36 (patch)
treed8664b324dc947f368e640d56c0beb8aeb9dd75e /source3
parent211697ee6cbeb2bbe86b96e17b922dd16d45c0e9 (diff)
downloadsamba-7a0bc5322114d792c5bce0f8f3d5f6a060670e36.tar.gz
samba-7a0bc5322114d792c5bce0f8f3d5f6a060670e36.tar.bz2
samba-7a0bc5322114d792c5bce0f8f3d5f6a060670e36.zip
- optimise tdb_store() a little
- prevent a free of an unallocated pointer (This used to be commit b35b5c63886823bb8b8d8c5c29ccc7b315a7de30)
Diffstat (limited to 'source3')
-rw-r--r--source3/tdb/tdb.c22
1 files 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;
}