1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-28 07:21:54 +03:00

- optimise tdb_store() a little

- prevent a free of an unallocated pointer
This commit is contained in:
Andrew Tridgell 0001-01-01 00:00:00 +00:00
parent 1ba42aca21
commit b35b5c6388

View File

@ -908,7 +908,7 @@ int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key)
unsigned hash; unsigned hash;
tdb_off offset, rec_ptr, last_ptr; tdb_off offset, rec_ptr, last_ptr;
struct list_struct rec, lastrec; struct list_struct rec, lastrec;
char *data; char *data = NULL;
/* find which hash bucket it is in */ /* find which hash bucket it is in */
hash = tdb_hash(&key); 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) int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
{ {
struct list_struct rec; struct list_struct rec;
char *data = NULL;
unsigned hash; unsigned hash;
tdb_off rec_ptr, offset; tdb_off rec_ptr, offset;
char *p = NULL;
/* find which hash bucket it is in */ /* find which hash bucket it is in */
hash = tdb_hash(&key); 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.full_hash = hash;
rec.magic = TDB_MAGIC; rec.magic = TDB_MAGIC;
/* write the new record */ p = (char *)malloc(sizeof(rec) + key.dsize + dbuf.dsize);
if (rec_write(tdb, rec_ptr, &rec) == -1) goto fail; if (!p) 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; 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 */ /* and point the top of the hash chain at it */
if (ofs_write(tdb, offset, &rec_ptr) == -1) goto fail; 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 #if TDB_DEBUG
printf("store failed for hash 0x%08x in bucket %u\n", hash, BUCKET(hash)); printf("store failed for hash 0x%08x in bucket %u\n", hash, BUCKET(hash));
#endif #endif
if (data) free(data); if (p) free(p);
tdb_unlock(tdb, BUCKET(hash)); tdb_unlock(tdb, BUCKET(hash));
return -1; return -1;
} }