From c9d98a8fc24f76d979ad479eff2fa66b31703832 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 3 Sep 2009 13:59:12 +0400 Subject: [PATCH] * Various bugfixes in map and radix code --- src/map.c | 73 +++++++++++++++++++++++++++++--------------------- src/mem_pool.c | 18 ++++++++----- src/radix.c | 43 ++++++----------------------- src/view.c | 2 +- 4 files changed, 63 insertions(+), 73 deletions(-) diff --git a/src/map.c b/src/map.c index 0202f448f..f19b822b6 100644 --- a/src/map.c +++ b/src/map.c @@ -317,7 +317,7 @@ add_map (const char *map_line, map_cb_t read_callback, map_fin_cb_t fin_callback def = map_line + sizeof ("file://") - 1; } else { - msg_err ("add_map: invalid map fetching protocol: %s", map_line); + msg_debug ("add_map: invalid map fetching protocol: %s", map_line); return FALSE; } /* Constant pool */ @@ -419,7 +419,7 @@ abstract_parse_list (memory_pool_t *pool, u_char *chunk, size_t len, struct map_ if (*p == '#') { if (s != str) { *s = '\0'; - s = memory_pool_strdup (pool, str); + s = memory_pool_strdup (pool, g_strstrip (str)); if (strlen (s) > 0) { func (data->cur_data, s, hash_fill); } @@ -431,7 +431,7 @@ abstract_parse_list (memory_pool_t *pool, u_char *chunk, size_t len, struct map_ else if (*p == '\r' || *p == '\n') { if (s != str) { *s = '\0'; - s = memory_pool_strdup (pool, str); + s = memory_pool_strdup (pool, g_strstrip (str)); if (strlen (s) > 0) { func (data->cur_data, s, hash_fill); } @@ -442,9 +442,6 @@ abstract_parse_list (memory_pool_t *pool, u_char *chunk, size_t len, struct map_ p ++; } } - else if (g_ascii_isspace (*p)) { - p ++; - } else { *s = *p; s ++; @@ -480,38 +477,52 @@ radix_tree_insert_helper (gpointer st, gconstpointer key, gpointer value) uint32_t mask = 0xFFFFFFFF; uint32_t ip; - char *token, *ipnet; + char *token, *ipnet, *err_str, **strv, **cur; struct in_addr ina; int k; - k = strlen ((char *)key) + 1; - ipnet = alloca (k); - g_strlcpy (ipnet, key, k); - token = strsep (&ipnet, "/"); - - if (ipnet != NULL) { - k = atoi (ipnet); - if (k > 32 || k < 0) { - msg_warn ("radix_tree_insert_helper: invalid netmask value: %d", k); - k = 32; + strv = g_strsplit_set ((char *)key, " ,;", 0); + cur = strv; + while (*cur) { + if (**cur == '\0') { + cur ++; + continue; + } + ipnet = *cur; + token = strsep (&ipnet, "/"); + + if (ipnet != NULL) { + errno = 0; + k = strtoul (ipnet, &err_str, 10); + if (errno != 0) { + msg_warn ("radix_tree_insert_helper: invalid netmask, error detected on symbol: %s, erorr: %s", err_str, strerror (errno)); + k = 32; + } + else if (k > 32 || k < 0) { + msg_warn ("radix_tree_insert_helper: invalid netmask value: %d", k); + k = 32; + } + k = 32 - k; + mask = mask << k; } - k = 32 - k; - mask = mask << k; - } - if (inet_aton (token, &ina) == 0) { - msg_err ("radix_tree_insert_helper: invalid ip address: %s", token); - return; - } + if (inet_aton (token, &ina) == 0) { + msg_err ("radix_tree_insert_helper: invalid ip address: %s", token); + return; + } - ip = ntohl ((uint32_t)ina.s_addr); - k = radix32tree_insert (tree, ip, mask, 1); - if (k == -1) { - msg_warn ("radix_tree_insert_helper: cannot insert ip to tree: %s, mask %X", inet_ntoa (ina), mask); - } - else if (k == 1) { - msg_warn ("add_ip_radix: ip %s, mask %X, value already exists", inet_ntoa (ina), mask); + ip = ntohl ((uint32_t)ina.s_addr); + k = radix32tree_insert (tree, ip, mask, 1); + if (k == -1) { + msg_warn ("radix_tree_insert_helper: cannot insert ip to tree: %s, mask %X", inet_ntoa (ina), mask); + } + else if (k == 1) { + msg_warn ("add_ip_radix: ip %s, mask %X, value already exists", inet_ntoa (ina), mask); + } + cur ++; } + + g_strfreev (strv); } u_char * diff --git a/src/mem_pool.c b/src/mem_pool.c index e8be6a95c..ce6eab86e 100644 --- a/src/mem_pool.c +++ b/src/mem_pool.c @@ -60,7 +60,13 @@ pool_chain_new (memory_pool_ssize_t size) g_assert (size > 0); chain = g_slice_alloc (sizeof (struct _pool_chain)); + + g_assert (chain != NULL); + chain->begin = g_slice_alloc (size); + + g_assert (chain->begin != NULL); + chain->len = size; chain->pos = chain->begin; chain->next = NULL; @@ -78,10 +84,9 @@ pool_chain_new_shared (memory_pool_ssize_t size) #if defined(HAVE_MMAP_ANON) chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0); + g_assert (chain != MAP_FAILED); chain->begin = ((u_char *)chain) + sizeof (struct _pool_chain_shared); - if (chain == MAP_FAILED) { - return NULL; - } + g_assert (chain->begin != MAP_FAILED); #elif defined(HAVE_MMAP_ZERO) int fd; @@ -90,10 +95,9 @@ pool_chain_new_shared (memory_pool_ssize_t size) return NULL; } chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + g_assert (chain != MAP_FAILED); chain->begin = ((u_char *)chain) + sizeof (struct _pool_chain_shared); - if (chain == MAP_FAILED) { - return NULL; - } + g_assert (chain->begin != MAP_FAILED); #else # error No mmap methods are defined #endif @@ -138,6 +142,8 @@ memory_pool_new (memory_pool_ssize_t size) } new = g_slice_alloc (sizeof (memory_pool_t)); + g_assert (new != NULL); + new->cur_pool = pool_chain_new (size); new->shared_pool = NULL; new->first_pool = new->cur_pool; diff --git a/src/radix.c b/src/radix.c index 8b9df48a7..e55e9e5ba 100644 --- a/src/radix.c +++ b/src/radix.c @@ -30,15 +30,16 @@ static void *radix_alloc (radix_tree_t *tree); radix_tree_t * -radix_tree_create (memory_pool_t *pool) +radix_tree_create () { radix_tree_t *tree; - tree = memory_pool_alloc (pool, sizeof(radix_tree_t)); + tree = g_malloc (sizeof(radix_tree_t)); if (tree == NULL) { return NULL; } - + + tree->pool = memory_pool_new (memory_pool_get_size ()); tree->size = 0; tree->root = radix_alloc (tree); @@ -50,7 +51,6 @@ radix_tree_create (memory_pool_t *pool) tree->root->left = NULL; tree->root->parent = NULL; tree->root->value = RADIX_NO_VALUE; - tree->pool = pool; return tree; } @@ -228,37 +228,10 @@ radix_alloc (radix_tree_t *tree) void radix_tree_free (radix_tree_t *tree) { - radix_node_t *node, *tmp; - - node = tree->root; - - for (;;) { - /* We are at the trie root and we have no more leaves, end of algorithm */ - if (!node->left && !node->right && !node->parent) { - break; - } - - /* Traverse to the end of trie */ - while (node->left || node->right) { - if (node->left) { - node = node->left; - } - else { - node = node->right; - } - } - /* Found leaf node, free it */ - if (node->parent->right == node) { - node->parent->right = NULL; - - } else { - node->parent->left = NULL; - } - - tmp = node; - /* Go up */ - node = node->parent; - } + + g_return_if_fail (tree != NULL); + memory_pool_delete (tree->pool); + g_free (tree); } /* diff --git a/src/view.c b/src/view.c index ec10eb120..efad70666 100644 --- a/src/view.c +++ b/src/view.c @@ -93,7 +93,7 @@ add_view_symbols (struct rspamd_view *view, char *line) gboolean add_view_ip (struct rspamd_view *view, char *line) { - if (add_map (line, read_radix_list, fin_radix_list, (void **)&view->symbols_hash)) { + if (add_map (line, read_radix_list, fin_radix_list, (void **)&view->ip_tree)) { return TRUE; } -- 2.39.5