]> git.ozlabs.org Git - ccan/commitdiff
tdb2: compare the extra 11 hash bits in the header.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 1 Dec 2010 13:10:05 +0000 (23:40 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 1 Dec 2010 13:10:05 +0000 (23:40 +1030)
We already have 10 hash bits encoded in the offset itself; we only get
here incorrectly about 1 time in 1000, so it's a pretty minor
optimization at best.

Nonetheless, we have the information, so let's check it before
accessing the key.  This reduces the probability of a false keycmp by
another factor of 2000.

ccan/tdb2/hash.c

index d4d08e420d878f6e35f60fac9cac1bbbcb5a840f..7ade36d35d24521c8e62645b33da35d9d373fda4 100644 (file)
@@ -108,12 +108,16 @@ static bool match(struct tdb_context *tdb,
        if (tdb_read_convert(tdb, off, rec, sizeof(*rec)) == -1)
                return ret;
 
        if (tdb_read_convert(tdb, off, rec, sizeof(*rec)) == -1)
                return ret;
 
-       /* FIXME: check extra bits in header? */
        if (rec_key_length(rec) != key->dsize) {
                add_stat(tdb, compare_wrong_keylen, 1);
                return ret;
        }
 
        if (rec_key_length(rec) != key->dsize) {
                add_stat(tdb, compare_wrong_keylen, 1);
                return ret;
        }
 
+       if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) {
+               add_stat(tdb, compare_wrong_rechash, 1);
+               return false;
+       }
+
        rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false);
        if (!rkey)
                return ret;
        rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false);
        if (!rkey)
                return ret;