diff options
| author | tedu <> | 2015-11-18 17:59:56 +0000 |
|---|---|---|
| committer | tedu <> | 2015-11-18 17:59:56 +0000 |
| commit | c64c447d13d9a6348155715e10403137c175acbd (patch) | |
| tree | e48c8144c158fc25f1dbf58e11baa543aa76fd6b /src/lib/libc/stdlib/icdb.c | |
| parent | 26abb4553b03e3297d6d18bdfaead144c236d71f (diff) | |
| download | openbsd-c64c447d13d9a6348155715e10403137c175acbd.tar.gz openbsd-c64c447d13d9a6348155715e10403137c175acbd.tar.bz2 openbsd-c64c447d13d9a6348155715e10403137c175acbd.zip | |
add a version field to prevent mayhem if different data gets stored
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/stdlib/icdb.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/lib/libc/stdlib/icdb.c b/src/lib/libc/stdlib/icdb.c index 077c59a1e4..35747c065f 100644 --- a/src/lib/libc/stdlib/icdb.c +++ b/src/lib/libc/stdlib/icdb.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: icdb.c,v 1.1 2015/11/18 16:46:49 tedu Exp $ */ | 1 | /* $OpenBSD: icdb.c,v 1.2 2015/11/18 17:59:56 tedu Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org> | 3 | * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org> |
| 4 | * | 4 | * |
| @@ -77,6 +77,7 @@ | |||
| 77 | */ | 77 | */ |
| 78 | struct icdbinfo { | 78 | struct icdbinfo { |
| 79 | uint32_t magic; /* magic */ | 79 | uint32_t magic; /* magic */ |
| 80 | uint32_t version; /* user specified version */ | ||
| 80 | uint32_t nentries; /* number of entries stored */ | 81 | uint32_t nentries; /* number of entries stored */ |
| 81 | uint32_t entrysize; /* size of each entry */ | 82 | uint32_t entrysize; /* size of each entry */ |
| 82 | uint32_t indexsize; /* number of entries in hash index */ | 83 | uint32_t indexsize; /* number of entries in hash index */ |
| @@ -112,7 +113,7 @@ roundup(uint32_t num) | |||
| 112 | } | 113 | } |
| 113 | 114 | ||
| 114 | struct icdb * | 115 | struct icdb * |
| 115 | icdb_new(uint32_t nentries, uint32_t entrysize, | 116 | icdb_new(uint32_t version, uint32_t nentries, uint32_t entrysize, |
| 116 | uint32_t nkeys, uint32_t *keysizes, uint32_t *keyoffsets) | 117 | uint32_t nkeys, uint32_t *keysizes, uint32_t *keyoffsets) |
| 117 | { | 118 | { |
| 118 | struct icdb *db; | 119 | struct icdb *db; |
| @@ -133,6 +134,7 @@ icdb_new(uint32_t nentries, uint32_t entrysize, | |||
| 133 | db->info = info; | 134 | db->info = info; |
| 134 | db->fd = -1; | 135 | db->fd = -1; |
| 135 | info->magic = magic; | 136 | info->magic = magic; |
| 137 | info->version = version; | ||
| 136 | if (nentries) | 138 | if (nentries) |
| 137 | if ((db->entries = reallocarray(NULL, nentries, entrysize))) | 139 | if ((db->entries = reallocarray(NULL, nentries, entrysize))) |
| 138 | db->allocated = nentries; | 140 | db->allocated = nentries; |
| @@ -146,7 +148,7 @@ icdb_new(uint32_t nentries, uint32_t entrysize, | |||
| 146 | } | 148 | } |
| 147 | 149 | ||
| 148 | struct icdb * | 150 | struct icdb * |
| 149 | icdb_open(const char *name, int flags) | 151 | icdb_open(const char *name, int flags, uint32_t version) |
| 150 | { | 152 | { |
| 151 | struct icdb *db = NULL; | 153 | struct icdb *db = NULL; |
| 152 | struct icdbinfo *info; | 154 | struct icdbinfo *info; |
| @@ -166,6 +168,8 @@ icdb_open(const char *name, int flags) | |||
| 166 | info = (struct icdbinfo *)ptr; | 168 | info = (struct icdbinfo *)ptr; |
| 167 | if (info->magic != magic) | 169 | if (info->magic != magic) |
| 168 | goto fail; | 170 | goto fail; |
| 171 | if (info->version != version) | ||
| 172 | goto fail; | ||
| 169 | 173 | ||
| 170 | if (!(db = calloc(1, sizeof(*db)))) | 174 | if (!(db = calloc(1, sizeof(*db)))) |
| 171 | goto fail; | 175 | goto fail; |
