From 2eaf8f7ce0ced2b5660f41a169447a8520bcc943 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Thu, 29 May 2014 08:02:15 -0600 Subject: Add freelist assertion on every free(). This commit performs a check on the freelist pages to ensure that a double free can never happen. --- db_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'db_test.go') diff --git a/db_test.go b/db_test.go index e1c4aaa..691c8fb 100644 --- a/db_test.go +++ b/db_test.go @@ -335,6 +335,52 @@ func TestMeta_validate_version(t *testing.T) { assert.Equal(t, m.validate(), ErrVersionMismatch) } +// Ensure that a DB in strict mode will fail when corrupted. +func TestDB_StrictMode(t *testing.T) { + var msg string + func() { + defer func() { + msg = fmt.Sprintf("%s", recover()) + }() + + withOpenDB(func(db *DB, path string) { + db.StrictMode = true + db.Update(func(tx *Tx) error { + tx.CreateBucket([]byte("foo")) + + // Corrupt the DB by extending the high water mark. + tx.meta.pgid++ + + return nil + }) + }) + }() + + assert.Equal(t, "check fail: page 4: unreachable unfreed", msg) +} + +// Ensure that a double freeing a page will result in a panic. +func TestDB_DoubleFree(t *testing.T) { + var msg string + func() { + defer func() { + msg = fmt.Sprintf("%s", recover()) + }() + withOpenDB(func(db *DB, path string) { + db.Update(func(tx *Tx) error { + tx.CreateBucket([]byte("foo")) + + // Corrupt the DB by adding a page to the freelist. + db.freelist.free(0, tx.page(3)) + + return nil + }) + }) + }() + + assert.Equal(t, "tx 2: page 3 already freed in tx 0", msg) +} + func ExampleDB_Update() { // Open the database. db, _ := Open(tempfile(), 0666) -- cgit v1.2.3