From 7a4ef21e6018ada49d52124bb714f81f55791e4b Mon Sep 17 00:00:00 2001 From: tedu <> Date: Wed, 18 Nov 2015 17:59:56 +0000 Subject: [PATCH] add a version field to prevent mayhem if different data gets stored --- src/include/icdb.h | 6 +++--- src/lib/libc/stdlib/icdb.c | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/include/icdb.h b/src/include/icdb.h index 6960546d..feb3c393 100644 --- a/src/include/icdb.h +++ b/src/include/icdb.h @@ -1,4 +1,4 @@ -/* $OpenBSD: icdb.h,v 1.1 2015/11/18 16:46:49 tedu Exp $ */ +/* $OpenBSD: icdb.h,v 1.2 2015/11/18 17:59:56 tedu Exp $ */ /* * Copyright (c) 2015 Ted Unangst * @@ -18,10 +18,10 @@ struct icdb; -struct icdb *icdb_new(uint32_t nentries, uint32_t entrysize, +struct icdb *icdb_new(uint32_t version, uint32_t nentries, uint32_t entrysize, uint32_t nkeys, uint32_t *keysizes, uint32_t *keyoffsets); -struct icdb *icdb_open(const char *name, int flags); +struct icdb *icdb_open(const char *name, int flags, uint32_t version); int icdb_get(struct icdb *db, void *entry, uint32_t idx); int icdb_lookup(struct icdb *db, int keynum, const void *key, void *entry, uint32_t *idxp); diff --git a/src/lib/libc/stdlib/icdb.c b/src/lib/libc/stdlib/icdb.c index 077c59a1..35747c06 100644 --- a/src/lib/libc/stdlib/icdb.c +++ b/src/lib/libc/stdlib/icdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: icdb.c,v 1.1 2015/11/18 16:46:49 tedu Exp $ */ +/* $OpenBSD: icdb.c,v 1.2 2015/11/18 17:59:56 tedu Exp $ */ /* * Copyright (c) 2015 Ted Unangst * @@ -77,6 +77,7 @@ */ struct icdbinfo { uint32_t magic; /* magic */ + uint32_t version; /* user specified version */ uint32_t nentries; /* number of entries stored */ uint32_t entrysize; /* size of each entry */ uint32_t indexsize; /* number of entries in hash index */ @@ -112,7 +113,7 @@ roundup(uint32_t num) } struct icdb * -icdb_new(uint32_t nentries, uint32_t entrysize, +icdb_new(uint32_t version, uint32_t nentries, uint32_t entrysize, uint32_t nkeys, uint32_t *keysizes, uint32_t *keyoffsets) { struct icdb *db; @@ -133,6 +134,7 @@ icdb_new(uint32_t nentries, uint32_t entrysize, db->info = info; db->fd = -1; info->magic = magic; + info->version = version; if (nentries) if ((db->entries = reallocarray(NULL, nentries, entrysize))) db->allocated = nentries; @@ -146,7 +148,7 @@ icdb_new(uint32_t nentries, uint32_t entrysize, } struct icdb * -icdb_open(const char *name, int flags) +icdb_open(const char *name, int flags, uint32_t version) { struct icdb *db = NULL; struct icdbinfo *info; @@ -166,6 +168,8 @@ icdb_open(const char *name, int flags) info = (struct icdbinfo *)ptr; if (info->magic != magic) goto fail; + if (info->version != version) + goto fail; if (!(db = calloc(1, sizeof(*db)))) goto fail;