Skip to content

Commit

Permalink
[libvalor] [dbgen]: add warning to dbgen about small capacity, add mo…
Browse files Browse the repository at this point in the history
…re checks and asserts for memory-related issues, fix memory leak database_read function
  • Loading branch information
0xf104a committed Nov 8, 2024
1 parent c6fd5d2 commit e9e9279
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion dbgen/src/dbgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 3 additions & 4 deletions libvalor/src/db/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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;
}
}
8 changes: 6 additions & 2 deletions libvalor/src/hashset.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string.h>
#include <assert.h>

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;
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -153,4 +157,4 @@ void destroy_stringset(stringset_t * set){
}
}
free(set);
}
}

0 comments on commit e9e9279

Please sign in to comment.