From 398d0c2929026fccb3409316720a4dcad225ab05 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 28 Aug 2009 11:56:34 +0930 Subject: lib/tdb: don't overwrite TDBs with different version numbers. In future, this may happen, and we don't want to clobber them. Signed-off-by: Rusty Russell --- lib/tdb/common/open.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/tdb/common/open.c b/lib/tdb/common/open.c index 2e6a707497b..141e6fe517d 100644 --- a/lib/tdb/common/open.c +++ b/lib/tdb/common/open.c @@ -240,17 +240,19 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags, errno = 0; if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header) - || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0 - || (tdb->header.version != TDB_VERSION - && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) { - /* its not a valid database - possibly initialise it */ + || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) { if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) { if (errno == 0) { - errno = EIO; /* ie bad format or something */ + errno = EIO; /* ie bad format or something */ } goto fail; } rev = (tdb->flags & TDB_CONVERT); + } else if (tdb->header.version != TDB_VERSION + && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION)))) { + /* wrong version */ + errno = EIO; + goto fail; } vp = (unsigned char *)&tdb->header.version; vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) | -- cgit