From e9e9279dc516a9c1b59a72cdbfb47ed132d833bf Mon Sep 17 00:00:00 2001 From: f104a Date: Fri, 8 Nov 2024 16:50:44 +0100 Subject: [PATCH] [libvalor] [dbgen]: add warning to dbgen about small capacity, add more checks and asserts for memory-related issues, fix memory leak database_read function --- dbgen/src/dbgen.c | 5 ++++- libvalor/src/db/db.c | 7 +++---- libvalor/src/hashset.c | 8 ++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/dbgen/src/dbgen.c b/dbgen/src/dbgen.c index 0335468..7b1f438 100644 --- a/dbgen/src/dbgen.c +++ b/dbgen/src/dbgen.c @@ -18,7 +18,7 @@ int main(int argc, const char *argv[]) { arguments_set_usage("--db FILE [-h|--help] [--check-entry|--increment-version|--add-name NAME|--check-name NAME|--add-threat FILE|--check-threat FILE|--set-version VERSION]"); argument_add_compulsory("--db", "Path to database file", ARG_STR); argument_add("--capacity", "Capacity to pre-allocate in database structures", ARG_INT, - (argvalue)(int64_t)1, true, false); + (argvalue)(int64_t)128, true, false); argument_add("--filename", "Threat file", ARG_STR, (argvalue) NULL, false, false); argument_add("--check-entry", "Check whether file in database and exit.", ARG_BOOL, @@ -62,6 +62,9 @@ int main(int argc, const char *argv[]) { if(capacity < 0){ die("Invalid capacity"); } + if(capacity < 16){ + warning("Capacity is too small. This may lead to performance and stability issues"); + } db = create_database(capacity); fclose(db_file); } diff --git a/libvalor/src/db/db.c b/libvalor/src/db/db.c index 37dde28..6d4960a 100644 --- a/libvalor/src/db/db.c +++ b/libvalor/src/db/db.c @@ -60,6 +60,7 @@ void database_add_name(database_t *db, const char *name) { } bool database_check_name(database_t *db, const char *name) { + assert(db); if(name == NULL){ fprintf(stderr, "Name passed to database_check_name is NULL!"); return false; @@ -69,7 +70,6 @@ bool database_check_name(database_t *db, const char *name) { void database_save(FILE *file, database_t *db) { size_t i = 0; - //printf("database_save: entered\n"); // Write metadata fwrite(&db->version, sizeof(db->version), 1, file); fwrite(&db->modulo, sizeof(db->modulo), 1, file); @@ -82,10 +82,8 @@ void database_save(FILE *file, database_t *db) { fwrite(str, sizeof(char), len, file); } // Write hashtree - //printf("Writing hashtree\n"); uint64_t tree_sz = tree_size(db->hash_tree); uint8_t* serialized_tree = serialize_ed_tree(db->hash_tree); - //printf("Serialize OK\n"); fwrite(serialized_tree, sizeof(uint8_t) * tree_sz, 1, file); // Clean-up free(serialized_tree); @@ -111,6 +109,7 @@ void database_read(FILE *file, database_t *db) { str[len] = (char) 0; fread(str, sizeof(char), len, file); database_add_name(db, str); + free(str); // String copied in add_name, so we do not need anymore } // Read hashtree uint8_t* serialized_tree = (uint8_t*)malloc(sizeof(uint8_t) * tree_size_for_depth(FUZZY_MAX_RESULT)); @@ -122,4 +121,4 @@ database_t *database_from_file(FILE *file) { database_t *db = (database_t *) malloc(sizeof(database_t)); database_read(file, db); return db; -} \ No newline at end of file +} diff --git a/libvalor/src/hashset.c b/libvalor/src/hashset.c index 39b9c97..48c2623 100644 --- a/libvalor/src/hashset.c +++ b/libvalor/src/hashset.c @@ -4,7 +4,7 @@ #include #include -unsigned long hash_djb2(const unsigned char *str) //djb2 hash by Dan Bernstein +static unsigned long hash_djb2(const unsigned char *str) //djb2 hash by Dan Bernstein { unsigned long hash = 5381; int c; @@ -104,6 +104,8 @@ stringset_t* create_stringset(uint32_t capacity){ void stringset_add(stringset_t* set, const char* str){ + assert(set); + assert(str); uint32_t hash = hash_djb2((unsigned char*) str); assert(set->capacity > 0); uint32_t index = hash % set->capacity; @@ -126,6 +128,8 @@ void stringset_add(stringset_t* set, const char* str){ } bool stringset_check(stringset_t* set, const char* str){ + assert(set); + assert(str); uint32_t hash = hash_djb2((unsigned char*) str); uint32_t index = hash % set->capacity; stringset_node_t* current_node = set->node_table[index]; @@ -153,4 +157,4 @@ void destroy_stringset(stringset_t * set){ } } free(set); -} \ No newline at end of file +}