From b10598ebdad2302c637c14961525c0d9cef937f5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 3 Nov 2010 10:47:39 +1030 Subject: [PATCH] tdb: fix warnings with gcc -O3, and one test crash. 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 | 14 +++++++++++--- ccan/tdb/test/run-corrupt.c | 6 ++++-- ccan/tdb/test/run-die-during-transaction.c | 8 ++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ccan/tdb/io.c b/ccan/tdb/io.c index e56be6fa..2262eabf 100644 --- a/ccan/tdb/io.c +++ b/ccan/tdb/io.c @@ -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; } diff --git a/ccan/tdb/test/run-corrupt.c b/ccan/tdb/test/run-corrupt.c index 2109e936..6f753ea6 100644 --- a/ccan/tdb/test/run-corrupt.c +++ b/ccan/tdb/test/run-corrupt.c @@ -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"); } } diff --git a/ccan/tdb/test/run-die-during-transaction.c b/ccan/tdb/test/run-die-during-transaction.c index b8943602..aa6b40f6 100644 --- a/ccan/tdb/test/run-die-during-transaction.c +++ b/ccan/tdb/test/run-die-during-transaction.c @@ -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; -- 2.39.2