From a0f46daa4a2db9a5ac522c24f5941198d68d4d57 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 3 Nov 2011 13:42:59 +0100 Subject: [PATCH] dlhist.c: store number of entries in header instead of table size Now that table size can be calculated, lets store the number of entries instead of size in the header so we can rely on that when reading entries, instead of the actual size on disk. this is safer if data is appended to the file outside of the application. --- dlhist.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/dlhist.c b/dlhist.c index 0411a21..fd74160 100644 --- a/dlhist.c +++ b/dlhist.c @@ -42,7 +42,7 @@ struct header { unsigned int signature; unsigned int version; - unsigned int size; + unsigned int entries; }; union hash { @@ -168,14 +168,14 @@ static void resize_table() { free(old); } -static void build_table(const char *buf, size_t len) { +static void build_table(const char *buf, size_t entries) { - size_t offset = 0; + size_t i, offset = 0; - table_size = calculate_size(len / HE_SZ); + table_size = calculate_size(entries); table = calloc(sizeof(*table), table_size); - while(offset < len) { + for(i=0; i < entries; i++) { struct hash_entry entry; memcpy(&entry.hash, buf + offset, sizeof(entry.hash)); @@ -194,7 +194,8 @@ static void build_table(const char *buf, size_t len) { int dlhist_open() { char filename[4096], *buf = NULL; - int ret = -1, fd = -1, offset = 0; + int ret = -1, fd = -1; + size_t entries = 0, offset = 0; struct stat st; struct header *hdr; @@ -228,10 +229,20 @@ int dlhist_open() { goto error; } + entries = htonl(hdr->entries); + offset = sizeof(*hdr); } - build_table(buf + offset, st.st_size - offset); + if (entries * HE_SZ > st.st_size - offset) { + fprintf(stderr, + "dlhist_open: file truncated. " + "expected atleast '%lu' bytes, got '%lu'\n", + entries * HE_SZ, st.st_size - offset); + goto error; + } + + build_table(buf + offset, entries); ret = 0; error: @@ -303,7 +314,7 @@ void dlhist_flush() { /* Write header */ hdr.signature = htonl(SIGNATURE); hdr.version = htonl(1); - hdr.size = htonl(table_size); + hdr.entries = htonl(table_count); write(fd, &hdr, sizeof(hdr));