[SCM] Samba Shared Repository - branch master updated

Volker Lendecke vlendec at samba.org
Mon Dec 19 08:54:03 MST 2011


The branch, master has been updated
       via  bfc7481 tdb2: Avoid a malloc/memcpy in _tdb1_store
       via  664add1 tdb: Avoid a malloc/memcpy in _tdb_store
      from  f39426c s4-dsdb: Relax the conditions where we can't do a subtree delete

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit bfc74811d04c5fa003f04947e573ee98fc510b13
Author: Volker Lendecke <vl at samba.org>
Date:   Thu Dec 15 10:50:34 2011 +0100

    tdb2: Avoid a malloc/memcpy in _tdb1_store
    
    Autobuild-User: Volker Lendecke <vlendec at samba.org>
    Autobuild-Date: Mon Dec 19 16:53:40 CET 2011 on sn-devel-104

commit 664add17757836c5ee98618aef11371f412b6e44
Author: Volker Lendecke <vl at samba.org>
Date:   Thu Dec 15 10:50:34 2011 +0100

    tdb: Avoid a malloc/memcpy in _tdb_store

-----------------------------------------------------------------------

Summary of changes:
 lib/tdb/common/tdb.c |   25 ++++++++-----------------
 lib/tdb2/tdb1_tdb.c  |   27 ++++++++-------------------
 2 files changed, 16 insertions(+), 36 deletions(-)


Changeset truncated at 500 lines:

diff --git a/lib/tdb/common/tdb.c b/lib/tdb/common/tdb.c
index 3a3c99c..ac2a482 100644
--- a/lib/tdb/common/tdb.c
+++ b/lib/tdb/common/tdb.c
@@ -473,7 +473,6 @@ static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
 {
 	struct tdb_record rec;
 	tdb_off_t rec_ptr;
-	char *p = NULL;
 	int ret = -1;
 
 	/* check for it existing, on insert. */
@@ -503,18 +502,6 @@ static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
 	if (flag != TDB_INSERT)
 		tdb_delete_hash(tdb, key, hash);
 
-	/* Copy key+value *before* allocating free space in case malloc
-	   fails and we are left with a dead spot in the tdb. */
-
-	if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
-		tdb->ecode = TDB_ERR_OOM;
-		goto fail;
-	}
-
-	memcpy(p, key.dptr, key.dsize);
-	if (dbuf.dsize)
-		memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
-
 	if (tdb->max_dead_records != 0) {
 		/*
 		 * Allow for some dead records per hash chain, look if we can
@@ -534,7 +521,10 @@ static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
 			if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
 			    || tdb->methods->tdb_write(
 				    tdb, rec_ptr + sizeof(rec),
-				    p, key.dsize + dbuf.dsize) == -1) {
+				    key.dptr, key.dsize) == -1
+			    || tdb->methods->tdb_write(
+				    tdb, rec_ptr + sizeof(rec) + key.dsize,
+				    dbuf.dptr, dbuf.dsize) == -1) {
 				goto fail;
 			}
 			goto done;
@@ -577,7 +567,10 @@ static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
 
 	/* write out and point the top of the hash chain at it */
 	if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
-	    || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
+	    || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec),
+				       key.dptr, key.dsize) == -1
+	    || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec)+key.dsize,
+				       dbuf.dptr, dbuf.dsize) == -1
 	    || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
 		/* Need to tdb_unallocate() here */
 		goto fail;
@@ -589,8 +582,6 @@ static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
 	if (ret == 0) {
 		tdb_increment_seqnum(tdb);
 	}
-
-	SAFE_FREE(p); 
 	return ret;
 }
 
diff --git a/lib/tdb2/tdb1_tdb.c b/lib/tdb2/tdb1_tdb.c
index 6f6f080..a220f47 100644
--- a/lib/tdb2/tdb1_tdb.c
+++ b/lib/tdb2/tdb1_tdb.c
@@ -478,7 +478,6 @@ static int _tdb1_store(struct tdb_context *tdb, TDB_DATA key,
 {
 	struct tdb1_record rec;
 	tdb1_off_t rec_ptr;
-	char *p = NULL;
 	int ret = -1;
 
 	/* check for it existing, on insert. */
@@ -515,20 +514,6 @@ static int _tdb1_store(struct tdb_context *tdb, TDB_DATA key,
 	if (flag != TDB_INSERT)
 		tdb1_delete_hash(tdb, key, hash);
 
-	/* Copy key+value *before* allocating free space in case malloc
-	   fails and we are left with a dead spot in the tdb. */
-
-	if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
-		tdb->last_error = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
-					     "tdb1_store: out of memory"
-					     " allocating copy");
-		goto fail;
-	}
-
-	memcpy(p, key.dptr, key.dsize);
-	if (dbuf.dsize)
-		memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
-
 	if (tdb->tdb1.max_dead_records != 0) {
 		/*
 		 * Allow for some dead records per hash chain, look if we can
@@ -548,7 +533,10 @@ static int _tdb1_store(struct tdb_context *tdb, TDB_DATA key,
 			if (tdb1_rec_write(tdb, rec_ptr, &rec) == -1
 			    || tdb->tdb1.io->tdb1_write(
 				    tdb, rec_ptr + sizeof(rec),
-				    p, key.dsize + dbuf.dsize) == -1) {
+				    key.dptr, key.dsize) == -1
+			    || tdb->tdb1.io->tdb1_write(
+				    tdb, rec_ptr + sizeof(rec) + key.dsize,
+				    dbuf.dptr, dbuf.dsize) == -1) {
 				goto fail;
 			}
 			goto done;
@@ -591,7 +579,10 @@ static int _tdb1_store(struct tdb_context *tdb, TDB_DATA key,
 
 	/* write out and point the top of the hash chain at it */
 	if (tdb1_rec_write(tdb, rec_ptr, &rec) == -1
-	    || tdb->tdb1.io->tdb1_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
+	    || tdb->tdb1.io->tdb1_write(tdb, rec_ptr + sizeof(rec),
+					key.dptr, key.dsize) == -1
+	    || tdb->tdb1.io->tdb1_write(tdb, rec_ptr + sizeof(rec) + key.dsize,
+					dbuf.dptr, dbuf.dsize) == -1
 	    || tdb1_ofs_write(tdb, TDB1_HASH_TOP(hash), &rec_ptr) == -1) {
 		/* Need to tdb1_unallocate() here */
 		goto fail;
@@ -603,8 +594,6 @@ static int _tdb1_store(struct tdb_context *tdb, TDB_DATA key,
 	if (ret == 0) {
 		tdb1_increment_seqnum(tdb);
 	}
-
-	SAFE_FREE(p);
 	return ret;
 }
 


-- 
Samba Shared Repository


More information about the samba-cvs mailing list