tdb: fix warnings with gcc -O3, and one test crash.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 3 Nov 2010 00:17:39 +0000 (10:47 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 3 Nov 2010 00:17:39 +0000 (10:47 +1030)
unsigned char * is allowed to alias, so we use that for byte reversing
rather than uint32_t.  This is portable.

Remove warnings about ignoring pread/pwrite return values.

And initialize tdb before setjmp: with optimization, gcc validly saw this
as NULL and crashed.

ccan/tdb/io.c
ccan/tdb/test/run-corrupt.c
ccan/tdb/test/run-die-during-transaction.c

index e56be6fa38362e9b8666f34deb43c38b80831a13..2262eabf93b19daf524b3876d8b7f55c1fe882ea 100644 (file)
@@ -125,9 +125,17 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
 /* Endian conversion: we only ever deal with 4 byte quantities */
 void *tdb_convert(void *buf, uint32_t size)
 {
-       uint32_t i, *p = (uint32_t *)buf;
-       for (i = 0; i < size / 4; i++)
-               p[i] = TDB_BYTEREV(p[i]);
+       uint32_t i;
+       unsigned char *p = buf, tmp;
+
+       for (i = 0; i < size; i += 4) {
+               tmp = p[i];
+               p[i] = p[i+3];
+               p[i+3] = tmp;
+               tmp = p[i+1];
+               p[i+1] = p[i+2];
+               p[i+2] = tmp;
+       }
        return buf;
 }
 
index 2109e936b1bf77eb645ffc7627f50896ed6b436e..6f753ea6d46c81c9dbd3f9e388c1f3078c98e30e 100644 (file)
@@ -43,9 +43,11 @@ static void tdb_flip_bit(struct tdb_context *tdb, unsigned int bit)
                ((unsigned char *)tdb->map_ptr)[off] ^= mask;
        else {
                unsigned char c;
-               pread(tdb->fd, &c, 1, off);
+               if (pread(tdb->fd, &c, 1, off) != 1)
+                       err(1, "pread");
                c ^= mask;
-               pwrite(tdb->fd, &c, 1, off);
+               if (pwrite(tdb->fd, &c, 1, off) != 1)
+                       err(1, "pwrite");
        }
 }
 
index b8943602655f79c5c6a0bc9851321cc4467ee268..aa6b40f6bbad9eb5246d03334ff9b6c0a738cf9f 100644 (file)
@@ -98,6 +98,10 @@ static bool test_death(enum operation op, struct agent *agent)
 
        current = target = 0;
 reset:
+       unlink(TEST_DBNAME);
+       tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
+                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+
        if (setjmp(jmpbuf) != 0) {
                /* We're partway through.  Simulate our death. */
                close(tdb->fd);
@@ -153,10 +157,6 @@ reset:
                goto reset;
        }
 
-       unlink(TEST_DBNAME);
-       tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
-
        /* Put key for agent to fetch. */
        key.dsize = strlen(KEY_STRING);
        key.dptr = (void *)KEY_STRING;