Fix memory corruptions in ndpi_node handler

Crash occurred with clang version 6.0.0-1ubuntu2 on ubuntu 18
This commit is contained in:
emanuele-f
2019-08-29 20:13:19 +02:00
parent 2c4495e483
commit fce2cd9817
2 changed files with 9 additions and 8 deletions
+2 -2
View File
@@ -371,7 +371,7 @@ static void addDefaultPort(struct ndpi_detection_module_struct *ndpi_mod,
}
node->proto = def, node->default_port = port, node->customUserProto = customUserProto;
ret = *(ndpi_default_ports_tree_node_t**)ndpi_tsearch(node, (void*)root, ndpi_default_ports_tree_node_t_cmp); /* Add it to the tree */
ret = (ndpi_default_ports_tree_node_t*)ndpi_tsearch(node, (void*)root, ndpi_default_ports_tree_node_t_cmp); /* Add it to the tree */
if(ret != node) {
NDPI_LOG_DBG(ndpi_mod, "[NDPI] %s:%d found duplicate for port %u: overwriting it with new value\n",
@@ -401,7 +401,7 @@ static int removeDefaultPort(ndpi_port_range *range,
for(port=range->port_low; port<=range->port_high; port++) {
node.proto = def, node.default_port = port;
ret = *(ndpi_default_ports_tree_node_t**)ndpi_tdelete(&node, (void*)root,
ret = (ndpi_default_ports_tree_node_t*)ndpi_tdelete(&node, (void*)root,
ndpi_default_ports_tree_node_t_cmp); /* Add it to the tree */
if(ret != NULL) {
+7 -6
View File
@@ -84,7 +84,7 @@ void * ndpi_tsearch(const void *vkey, void **vrootp,
int r;
if((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
return ((void *)*rootp); /* we found it! */
return ((*rootp)->key); /* we found it! */
rootp = (r < 0) ?
&(*rootp)->left : /* T3: follow left branch */
&(*rootp)->right; /* T4: follow right branch */
@@ -95,7 +95,7 @@ void * ndpi_tsearch(const void *vkey, void **vrootp,
q->key = key; /* initialize new node */
q->left = q->right = (ndpi_node *)0;
}
return ((void *)q);
return ((void *)q->key);
}
/* delete node with given key */
@@ -104,15 +104,13 @@ void * ndpi_tdelete(const void *vkey, void **vrootp,
{
ndpi_node **rootp = (ndpi_node **)vrootp;
char *key = (char *)vkey;
ndpi_node *p = (ndpi_node *)1;
ndpi_node *q;
ndpi_node *r;
int cmp;
if(rootp == (ndpi_node **)0 || *rootp == (ndpi_node *)0)
return ((ndpi_node *)0);
return((void *)0);
while ((cmp = (*compar)(key, (*rootp)->key)) != 0) {
p = *rootp;
rootp = (cmp < 0) ?
&(*rootp)->left : /* follow left branch */
&(*rootp)->right; /* follow right branch */
@@ -134,9 +132,12 @@ void * ndpi_tdelete(const void *vkey, void **vrootp,
q->right = (*rootp)->right;
}
}
key = (*rootp)->key;
ndpi_free((ndpi_node *) *rootp); /* D4: Free node */
*rootp = q; /* link parent to new node */
return(p);
/* Return the key to give the caller a chance to free custom data */
return(key);
}
/* Walk the nodes of a tree */