From bb76c6a4d74036c9de5e75134554ca04b0b9b5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 20 Jan 2019 23:01:04 +0100 Subject: Fix some memmory errors. And discovered some new ones. --- hash.h | 6 +++++- hash_help.inc | 5 +++-- parse.c | 3 ++- parse.h | 2 -- strbuf.c | 9 ++++++--- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/hash.h b/hash.h index 2a095cfd..cf955a95 100644 --- a/hash.h +++ b/hash.h @@ -18,7 +18,11 @@ unsigned long hash(char*); typedef struct { int size; int item_count; - TYPE* values; + /* NOTE + * Hash maps are always assumed to hold pointers to objects + * Double pointer means a list of pointers. + */ + TYPE** values; } TABLE(TYPE); int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value ); diff --git a/hash_help.inc b/hash_help.inc index 19463be4..ddaf4ae0 100644 --- a/hash_help.inc +++ b/hash_help.inc @@ -5,7 +5,7 @@ int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value) { // TODO genicify the hash function unsigned long h = hash(value->key.mem) % table->size; - table->values[h] = *value; + table->values[h] = value; /* TODO conflict resolution */ @@ -25,7 +25,8 @@ int HASH_INIT(TYPE) ( TABLE(TYPE)* table, int init_size ) { TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) { unsigned long h = hash(key) % table->size; - TYPE* mem = (table->values + h); + // TYPE* mem = (table->values + h); + TYPE* mem = table->values[h]; if (mem == NULL) { return 0; } else if (strcmp(mem->key.mem, key) == 0) { diff --git a/parse.c b/parse.c index cce311a6..cd85ec53 100644 --- a/parse.c +++ b/parse.c @@ -27,7 +27,6 @@ int parse_file(FILE* f, vcalendar* cal) { int line = 0; - // TODO this can apparently segfault... NEW(vevent, ev, /**/ 100); content_line cline; @@ -86,10 +85,12 @@ int parse_file(FILE* f, vcalendar* cal) { /* We just got a value */ // LINE(line, key.mem, val.mem); + /* if (strbuf_c(&cline.key, "LOCATION")) { if (strbuf_c(&cline.val, "")) return 1; LINE(line, cline.key.mem, cline.val.mem); } + */ handle_kv(cal, ev, &cline, line, &s_ctx); strbuf_soft_reset(&str); p_ctx = p_key; diff --git a/parse.h b/parse.h index cb6c2d1e..424e88bf 100644 --- a/parse.h +++ b/parse.h @@ -1,8 +1,6 @@ #ifndef PARSE_H #define PARSE_H -#define SAFE_STR - #include #include diff --git a/strbuf.c b/strbuf.c index 877778a0..48702cf8 100644 --- a/strbuf.c +++ b/strbuf.c @@ -2,6 +2,9 @@ #include +#include +#define ERR(s) fprintf(stderr, "\x1B[0;31mERR\x1b[m (strbuf %3i): %s\n", __LINE__, s) + int init_string(string* str, size_t len) { str->mem = malloc(len); str->alloc = len; @@ -24,7 +27,7 @@ int realloc_string(string* str, size_t len) { int free_string(string* str) { #ifdef SAFE_STR - if (str->alloc == 0) { + if (str->alloc == 0 || str->mem == NULL) { ERR("String not allocated"); return 1; } @@ -69,7 +72,7 @@ char* charat(string* s, int idx) { #ifdef SAFE_STR if (idx > s->len) { ERR("Index out of bounds"); - return -1; + return (char*) -1; } #endif return &s->mem[idx]; @@ -88,7 +91,7 @@ int strbuf_reset(string* s) int strbuf_init_copy(string* dest, string* src) { #ifdef SAFE_STR if (dest->alloc != 0) { - ERR("Dest already allocated", -1); + ERR("Dest already allocated"); return 1; } #endif -- cgit v1.2.3