From 505520d673f9969d841d8894731d476bd30be5cf Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 23 Oct 2023 00:34:09 +0300 Subject: [PATCH] trie: limit key size --- src/map.h | 4 ++-- src/map_trie.c | 15 +++------------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/map.h b/src/map.h index 4126273..02f62d2 100644 --- a/src/map.h +++ b/src/map.h @@ -41,12 +41,12 @@ struct trie_node { struct trie_node *data[1]; }; typedef struct trie_node *map_t; +#define TRIE_MAX_KEY 256 struct trie_find { struct trie_node *n; void *value; size_t len; - size_t cap; - char key[1]; + char key[TRIE_MAX_KEY]; }; typedef struct trie_find *map_find_t; #define map_findfree(f) (free(*(f)), *(f) = 0) diff --git a/src/map_trie.c b/src/map_trie.c index 19723cc..5c28b20 100644 --- a/src/map_trie.c +++ b/src/map_trie.c @@ -44,6 +44,8 @@ void *map_get(map_t *root, const char *key) int map_set(map_t *root, const char *key, const void *value, void **oldvalue) { int rv = 0; + if (strlen(key)+1 > TRIE_MAX_KEY) + return -1; if (oldvalue) *oldvalue = NULL; struct trie_node *n = *root; @@ -139,16 +141,6 @@ int map_findnext(map_find_t *pf) { int ent = 0; // go to the first child node for (;;) { if (!trie_is_end(ent, f->n)) { - if (f->len == f->cap) { - f->cap *= 2; - void *nf = realloc(f, sizeof(*f)+f->cap); - if (!nf) { - *pf = NULL; - free(f); - return -1; - } - *pf = f = nf; - } f->n = f->n->data[ent]; f->key[f->len++] = f->n->key; if (f->n->value) { // found a value @@ -176,11 +168,10 @@ int map_findfirst(map_find_t *pf, map_t *root) { if (*pf) { f = *pf; } else { - f = *pf = malloc(sizeof(*f)+16); + f = *pf = malloc(sizeof(*f)); if (!f) return -1; f->len = 0; - f->cap = 16; } f->n = *root; if (f->n->value) { -- 2.56.0