Index: cherokee/avl.c =================================================================== --- cherokee/avl.c (revision 1832) +++ cherokee/avl.c (working copy) @@ -71,6 +71,9 @@ static ret_t node_free (cherokee_avl_node_t *node) { + if (unlikely (node == NULL)) + return ret_ok; + cherokee_buffer_mrproper (&node->id); free (node); @@ -83,6 +86,9 @@ ret_t cherokee_avl_init (cherokee_avl_t *avl) { + if (unlikely (avl == NULL)) + return ret_error; + avl->root = NULL; avl->case_insensitive = false; @@ -119,6 +125,9 @@ ret_t cherokee_avl_free (cherokee_avl_t *avl, cherokee_func_free_t free_func) { + if (unlikely (avl == NULL)) + return ret_ok; + cherokee_avl_mrproper (avl, free_func); free (avl); return ret_ok; @@ -143,6 +152,9 @@ ret_t cherokee_avl_set_case (cherokee_avl_t *avl, cherokee_boolean_t case_insensitive) { + if (unlikely (avl == NULL)) + return ret_error; + avl->case_insensitive = case_insensitive; return ret_ok; } @@ -153,7 +165,7 @@ { cherokee_avl_node_t *tmp; - if (!avl->root) + if (unlikely (avl == NULL) || avl->root == NULL) return NULL; tmp = avl->root; @@ -167,8 +179,13 @@ static cherokee_avl_node_t * node_next (cherokee_avl_node_t *node) { - cherokee_avl_node_t *tmp = node->right; + cherokee_avl_node_t *tmp; + + if (unlikely (node == NULL)) + return NULL; + tmp = node->right; + if (node->right_child) while (tmp->left_child) tmp = tmp->left; @@ -178,8 +195,13 @@ static cherokee_avl_node_t * node_prev (cherokee_avl_node_t *node) { - cherokee_avl_node_t *tmp = node->left; + cherokee_avl_node_t *tmp; + if (unlikely (node == NULL)) + return NULL; + + tmp = node->left; + if (node->left_child) while (tmp->right_child) tmp = tmp->right; @@ -193,6 +215,9 @@ cherokee_avl_node_t *right; cint_t a_bal; cint_t b_bal; + + if (unlikely (node == NULL)) + return NULL; right = node->right; @@ -232,6 +257,9 @@ cint_t a_bal; cint_t b_bal; + if (unlikely (node == NULL)) + return NULL; + left = node->left; if (left->right_child) @@ -267,6 +295,9 @@ static cherokee_avl_node_t * node_balance (cherokee_avl_node_t *node) { + if (unlikely (node == NULL)) + return NULL; + if (node->balance < -1) { if (node->left->balance > 0) node->left = node_rotate_left (node->left); @@ -289,10 +320,17 @@ short re; cherokee_boolean_t is_left; cherokee_avl_node_t *path[MAX_HEIGHT]; - cherokee_avl_node_t *node = tree->root; - cherokee_avl_node_t *parent = NULL; - cint_t idx = 1; + cherokee_avl_node_t *node; + cherokee_avl_node_t *parent; + cint_t idx; + if (unlikely (tree == NULL)) + return; + + node = tree->root; + parent = NULL; + idx = 1; + path[0] = NULL; /* If the tree is empty.. @@ -376,8 +414,13 @@ cherokee_avl_add (cherokee_avl_t *avl, cherokee_buffer_t *key, void *value) { ret_t ret; - cherokee_avl_node_t *n = NULL; + cherokee_avl_node_t *n; + if (unlikely (avl == NULL)) + return ret_error; + + n = NULL; + if (unlikely (cherokee_buffer_is_empty(key))) return ret_error; @@ -401,15 +444,18 @@ cherokee_avl_node_t *path[MAX_HEIGHT]; cherokee_avl_node_t *parent; cherokee_avl_node_t *pbalance; - cherokee_avl_node_t *node = avl->root; - cint_t idx = 1; + cherokee_avl_node_t *node; + cint_t idx; - if (unlikely (cherokee_buffer_is_empty(key))) + if (unlikely (avl == NULL || cherokee_buffer_is_empty(key))) return ret_error; if (avl->root == NULL) return ret_not_found; + node = avl->root; + idx = 1; + path[0] = NULL; while (true) { @@ -568,13 +614,14 @@ short re; cherokee_avl_node_t *node; - if (unlikely (cherokee_buffer_is_empty(key))) + if (unlikely (avl == NULL || cherokee_buffer_is_empty(key))) return ret_error; - node = avl->root; - if (!node) + if (avl->root == NULL) return ret_not_found; + node = avl->root; + while (true) { re = compare_buffers (key, &node->id, avl->case_insensitive); if (re == 0) { @@ -607,6 +654,9 @@ { cherokee_buffer_t tmp_key; + if (unlikely(avl == NULL)) + return ret_error; + cherokee_buffer_fake (&tmp_key, (const char *)key, strlen(key)); return cherokee_avl_get (avl, &tmp_key, value); } @@ -617,6 +667,9 @@ { cherokee_buffer_t tmp_key; + if (unlikely(avl == NULL)) + return ret_error; + cherokee_buffer_fake (&tmp_key, (const char *)key, strlen(key)); return cherokee_avl_add (avl, &tmp_key, value); } @@ -627,6 +680,9 @@ { cherokee_buffer_t tmp_key; + if (unlikely(avl == NULL)) + return ret_error; + cherokee_buffer_fake (&tmp_key, (const char *)key, strlen(key)); return cherokee_avl_del (avl, &tmp_key, value); } @@ -636,8 +692,11 @@ cherokee_avl_while (cherokee_avl_t *avl, cherokee_avl_while_func_t func, void *param, cherokee_buffer_t **key, void **value) { ret_t ret; - cherokee_avl_node_t *node = avl->root; + cherokee_avl_node_t *node; + if (unlikely(avl == NULL)) + return ret_error; + if (avl->root == NULL) return ret_ok; @@ -670,6 +729,9 @@ ret_t cherokee_avl_len (cherokee_avl_t *avl, size_t *len) { + if (unlikely(avl == NULL)) + return ret_error; + *len = 0; return cherokee_avl_while (avl, func_len_each, len, NULL, NULL); } @@ -681,7 +743,7 @@ static void print_node (cherokee_avl_node_t *node) { - if (! node) { + if (node == NULL) { printf ("\n"); return; } @@ -697,6 +759,9 @@ ret_t cherokee_avl_print (cherokee_avl_t *avl) { + if (unlikely(avl == NULL)) + return ret_error; + print_node (avl->root); return ret_ok; } @@ -775,6 +840,9 @@ ret_t cherokee_avl_check (cherokee_avl_t *avl) { + if (unlikely (avl == NULL)) + return ret_error; + return node_check (avl->root); }