Browse Source

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.
Rusty Russell 15 years ago
parent
commit
b10598ebda
3 changed files with 19 additions and 9 deletions
  1. 11 3
      ccan/tdb/io.c
  2. 4 2
      ccan/tdb/test/run-corrupt.c
  3. 4 4
      ccan/tdb/test/run-die-during-transaction.c

+ 11 - 3
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 */
 /* Endian conversion: we only ever deal with 4 byte quantities */
 void *tdb_convert(void *buf, uint32_t size)
 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;
 	return buf;
 }
 }
 
 

+ 4 - 2
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;
 		((unsigned char *)tdb->map_ptr)[off] ^= mask;
 	else {
 	else {
 		unsigned char c;
 		unsigned char c;
-		pread(tdb->fd, &c, 1, off);
+		if (pread(tdb->fd, &c, 1, off) != 1)
+			err(1, "pread");
 		c ^= mask;
 		c ^= mask;
-		pwrite(tdb->fd, &c, 1, off);
+		if (pwrite(tdb->fd, &c, 1, off) != 1)
+			err(1, "pwrite");
 	}
 	}
 }
 }
 
 

+ 4 - 4
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;
 	current = target = 0;
 reset:
 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) {
 	if (setjmp(jmpbuf) != 0) {
 		/* We're partway through.  Simulate our death. */
 		/* We're partway through.  Simulate our death. */
 		close(tdb->fd);
 		close(tdb->fd);
@@ -153,10 +157,6 @@ reset:
 		goto 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. */
 	/* Put key for agent to fetch. */
 	key.dsize = strlen(KEY_STRING);
 	key.dsize = strlen(KEY_STRING);
 	key.dptr = (void *)KEY_STRING;
 	key.dptr = (void *)KEY_STRING;