Fix of rbtree lookup on hash collisions.

Previous code incorrectly assumed that nodes with identical keys are linked
together.  This might not be true after tree rebalance.

Patch by Lanshun Zhou.
This commit is contained in:
Maxim Dounin 2012-02-27 22:15:39 +00:00
parent 0709b4a272
commit eac588d9f4
6 changed files with 87 additions and 122 deletions

View file

@ -1142,7 +1142,6 @@ ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name,
/* hash == node->key */
do {
file = (ngx_cached_open_file_t *) node;
rc = ngx_strcmp(name->data, file->name);
@ -1152,10 +1151,6 @@ ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name,
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && hash == node->key);
break;
}
return NULL;

View file

@ -1689,7 +1689,6 @@ ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash)
/* hash == node->key */
do {
rn = (ngx_resolver_node_t *) node;
rc = ngx_memn2cmp(name->data, rn->name, name->len, rn->nlen);
@ -1699,10 +1698,6 @@ ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_t *name, uint32_t hash)
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && hash == node->key);
break;
}
/* not found */

View file

@ -1801,11 +1801,10 @@ ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn, u_char *id, int len,
/* hash == node->key */
do {
sess_id = (ngx_ssl_sess_id_t *) node;
rc = ngx_memn2cmp(id, sess_id->id,
(size_t) len, (size_t) node->data);
rc = ngx_memn2cmp(id, sess_id->id, (size_t) len, (size_t) node->data);
if (rc == 0) {
if (sess_id->expire > ngx_time()) {
@ -1835,10 +1834,6 @@ ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn, u_char *id, int len,
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && hash == node->key);
break;
}
done:
@ -1908,7 +1903,6 @@ ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
/* hash == node->key */
do {
sess_id = (ngx_ssl_sess_id_t *) node;
rc = ngx_memn2cmp(id, sess_id->id, len, (size_t) node->data);
@ -1929,10 +1923,6 @@ ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && hash == node->key);
break;
}
done:

View file

@ -325,7 +325,6 @@ ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree, ngx_http_variable_value_t *vv,
/* hash == node->key */
do {
lcn = (ngx_http_limit_conn_node_t *) &node->color;
rc = ngx_memn2cmp(vv->data, lcn->data,
@ -335,10 +334,6 @@ ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree, ngx_http_variable_value_t *vv,
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && hash == node->key);
break;
}
return NULL;

View file

@ -385,7 +385,6 @@ ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit, ngx_uint_t hash,
/* hash == node->key */
do {
lr = (ngx_http_limit_req_node_t *) &node->color;
rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
@ -422,10 +421,6 @@ ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit, ngx_uint_t hash,
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && hash == node->key);
break;
}
*ep = 0;

View file

@ -799,7 +799,6 @@ ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
/* node_key == node->key */
do {
fcn = (ngx_http_file_cache_node_t *) node;
rc = ngx_memcmp(&key[sizeof(ngx_rbtree_key_t)], fcn->key,
@ -810,10 +809,6 @@ ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && node_key == node->key);
break;
}
/* not found */