ctdb-common: Simplify code using local variables
authorAmitay Isaacs <amitay@gmail.com>
Fri, 2 Sep 2016 07:11:17 +0000 (17:11 +1000)
committerJeremy Allison <jra@samba.org>
Thu, 27 Oct 2016 21:53:12 +0000 (23:53 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/common/ctdb_ltdb.c

index 6c941d87e45624a2f0feebf47d56c7611d172421..4f7811d72f941de27bc600bc4b3385e601a94a5f 100644 (file)
@@ -171,6 +171,7 @@ int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key,
 {
        struct ctdb_context *ctdb = ctdb_db->ctdb;
        TDB_DATA rec;
+       uint32_t hsize = sizeof(struct ctdb_ltdb_header);
        int ret;
        bool seqnum_suppressed = false;
 
@@ -179,14 +180,20 @@ int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key,
        }
 
        if (ctdb->flags & CTDB_FLAG_TORTURE) {
+               TDB_DATA old;
                struct ctdb_ltdb_header *h2;
-               rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
-               h2 = (struct ctdb_ltdb_header *)rec.dptr;
-               if (rec.dptr && rec.dsize >= sizeof(h2) && h2->rsn > header->rsn) {
-                       DEBUG(DEBUG_CRIT,("RSN regression! %llu %llu\n",
-                                (unsigned long long)h2->rsn, (unsigned long long)header->rsn));
+
+               old = tdb_fetch(ctdb_db->ltdb->tdb, key);
+               h2 = (struct ctdb_ltdb_header *)old.dptr;
+               if (old.dptr != NULL && old.dsize >= hsize &&
+                   h2->rsn > header->rsn) {
+                       DEBUG(DEBUG_ERR,
+                             ("RSN regression! %"PRIu64" %"PRIu64"\n",
+                              h2->rsn, header->rsn));
+               }
+               if (old.dptr != NULL) {
+                       free(old.dptr);
                }
-               if (rec.dptr) free(rec.dptr);
        }
 
        rec.dsize = sizeof(*header) + data.dsize;
@@ -202,14 +209,14 @@ int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key,
                TDB_DATA old;
                old = tdb_fetch(ctdb_db->ltdb->tdb, key);
 
-               if ( (old.dsize == rec.dsize)
-               && !memcmp(old.dptr+sizeof(struct ctdb_ltdb_header),
-                         rec.dptr+sizeof(struct ctdb_ltdb_header),
-                         rec.dsize-sizeof(struct ctdb_ltdb_header)) ) {
+               if ((old.dsize == hsize + data.dsize) &&
+                   memcmp(old.dptr+hsize, data.dptr, data.dsize) == 0) {
                        tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_SEQNUM);
                        seqnum_suppressed = true;
                }
-               if (old.dptr) free(old.dptr);
+               if (old.dptr != NULL) {
+                       free(old.dptr);
+               }
        }
        ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
        if (ret != 0) {