Browse Source

add a version field to prevent mayhem if different data gets stored

OPENBSD_5_9
tedu 9 years ago
parent
commit
7a4ef21e60
2 changed files with 10 additions and 6 deletions
  1. +3
    -3
      src/include/icdb.h
  2. +7
    -3
      src/lib/libc/stdlib/icdb.c

+ 3
- 3
src/include/icdb.h View File

@ -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 <tedu@openbsd.org> * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
* *
@ -18,10 +18,10 @@
struct icdb; 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); 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_get(struct icdb *db, void *entry, uint32_t idx);
int icdb_lookup(struct icdb *db, int keynum, const void *key, void *entry, int icdb_lookup(struct icdb *db, int keynum, const void *key, void *entry,
uint32_t *idxp); uint32_t *idxp);


+ 7
- 3
src/lib/libc/stdlib/icdb.c View File

@ -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 <tedu@openbsd.org> * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
* *
@ -77,6 +77,7 @@
*/ */
struct icdbinfo { struct icdbinfo {
uint32_t magic; /* magic */ uint32_t magic; /* magic */
uint32_t version; /* user specified version */
uint32_t nentries; /* number of entries stored */ uint32_t nentries; /* number of entries stored */
uint32_t entrysize; /* size of each entry */ uint32_t entrysize; /* size of each entry */
uint32_t indexsize; /* number of entries in hash index */ uint32_t indexsize; /* number of entries in hash index */
@ -112,7 +113,7 @@ roundup(uint32_t num)
} }
struct icdb * 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) uint32_t nkeys, uint32_t *keysizes, uint32_t *keyoffsets)
{ {
struct icdb *db; struct icdb *db;
@ -133,6 +134,7 @@ icdb_new(uint32_t nentries, uint32_t entrysize,
db->info = info; db->info = info;
db->fd = -1; db->fd = -1;
info->magic = magic; info->magic = magic;
info->version = version;
if (nentries) if (nentries)
if ((db->entries = reallocarray(NULL, nentries, entrysize))) if ((db->entries = reallocarray(NULL, nentries, entrysize)))
db->allocated = nentries; db->allocated = nentries;
@ -146,7 +148,7 @@ icdb_new(uint32_t nentries, uint32_t entrysize,
} }
struct icdb * struct icdb *
icdb_open(const char *name, int flags)
icdb_open(const char *name, int flags, uint32_t version)
{ {
struct icdb *db = NULL; struct icdb *db = NULL;
struct icdbinfo *info; struct icdbinfo *info;
@ -166,6 +168,8 @@ icdb_open(const char *name, int flags)
info = (struct icdbinfo *)ptr; info = (struct icdbinfo *)ptr;
if (info->magic != magic) if (info->magic != magic)
goto fail; goto fail;
if (info->version != version)
goto fail;
if (!(db = calloc(1, sizeof(*db)))) if (!(db = calloc(1, sizeof(*db))))
goto fail; goto fail;


Loading…
Cancel
Save