2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-29 05:28:00 +00:00

Fix case insensitive matching in isc_ht hash table implementation

The case insensitive matching in isc_ht was basically completely broken
as only the hashvalue computation was case insensitive, but the key
comparison was always case sensitive.
This commit is contained in:
Ondřej Surý 2024-02-11 00:49:32 +01:00
parent fca10c1305
commit 175655b771
No known key found for this signature in database
GPG Key ID: 2820F37E873DEA41
2 changed files with 61 additions and 4 deletions

View File

@ -14,6 +14,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#include <isc/ascii.h>
#include <isc/hash.h> #include <isc/hash.h>
#include <isc/ht.h> #include <isc/ht.h>
#include <isc/magic.h> #include <isc/magic.h>
@ -95,9 +96,11 @@ isc__ht_iter_next(isc_ht_iter_t *it);
static bool static bool
isc__ht_node_match(isc_ht_node_t *node, const uint32_t hashval, isc__ht_node_match(isc_ht_node_t *node, const uint32_t hashval,
const uint8_t *key, uint32_t keysize) { const uint8_t *key, uint32_t keysize, bool case_sensitive) {
return (node->hashval == hashval && node->keysize == keysize && return (node->hashval == hashval && node->keysize == keysize &&
memcmp(node->key, key, keysize) == 0); (case_sensitive
? (memcmp(node->key, key, keysize) == 0)
: (isc_ascii_lowerequal(node->key, key, keysize))));
} }
static uint32_t static uint32_t
@ -338,7 +341,9 @@ nexttable:
for (isc_ht_node_t *node = ht->table[findex][hash]; node != NULL; for (isc_ht_node_t *node = ht->table[findex][hash]; node != NULL;
node = node->next) node = node->next)
{ {
if (isc__ht_node_match(node, hashval, key, keysize)) { if (isc__ht_node_match(node, hashval, key, keysize,
ht->case_sensitive))
{
return (node); return (node);
} }
} }
@ -385,7 +390,9 @@ isc__ht_delete(isc_ht_t *ht, const unsigned char *key, const uint32_t keysize,
for (isc_ht_node_t *node = ht->table[idx][hash]; node != NULL; for (isc_ht_node_t *node = ht->table[idx][hash]; node != NULL;
prev = node, node = node->next) prev = node, node = node->next)
{ {
if (isc__ht_node_match(node, hashval, key, keysize)) { if (isc__ht_node_match(node, hashval, key, keysize,
ht->case_sensitive))
{
if (prev == NULL) { if (prev == NULL) {
ht->table[idx][hash] = node->next; ht->table[idx][hash] = node->next;
} else { } else {

View File

@ -330,7 +330,57 @@ ISC_RUN_TEST_IMPL(isc_ht_iterator) {
test_ht_iterator(); test_ht_iterator();
} }
ISC_RUN_TEST_IMPL(isc_ht_case) {
isc_ht_t *ht = NULL;
void *f = NULL;
isc_result_t result = ISC_R_UNSET;
unsigned char lower[16] = { "test case" };
unsigned char same[16] = { "test case" };
unsigned char upper[16] = { "TEST CASE" };
unsigned char mixed[16] = { "tEsT CaSe" };
isc_ht_init(&ht, mctx, 8, ISC_HT_CASE_SENSITIVE);
assert_non_null(ht);
result = isc_ht_add(ht, lower, 16, (void *)lower);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_ht_add(ht, same, 16, (void *)same);
assert_int_equal(result, ISC_R_EXISTS);
result = isc_ht_add(ht, upper, 16, (void *)upper);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_ht_find(ht, mixed, 16, &f);
assert_int_equal(result, ISC_R_NOTFOUND);
assert_null(f);
isc_ht_destroy(&ht);
assert_null(ht);
isc_ht_init(&ht, mctx, 8, ISC_HT_CASE_INSENSITIVE);
assert_non_null(ht);
result = isc_ht_add(ht, lower, 16, (void *)lower);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_ht_add(ht, same, 16, (void *)same);
assert_int_equal(result, ISC_R_EXISTS);
result = isc_ht_add(ht, upper, 16, (void *)upper);
assert_int_equal(result, ISC_R_EXISTS);
result = isc_ht_find(ht, mixed, 16, &f);
assert_int_equal(result, ISC_R_SUCCESS);
assert_ptr_equal(f, &lower);
isc_ht_destroy(&ht);
assert_null(ht);
}
ISC_TEST_LIST_START ISC_TEST_LIST_START
ISC_TEST_ENTRY(isc_ht_case)
ISC_TEST_ENTRY(isc_ht_1_120) ISC_TEST_ENTRY(isc_ht_1_120)
ISC_TEST_ENTRY(isc_ht_6_1000) ISC_TEST_ENTRY(isc_ht_6_1000)
ISC_TEST_ENTRY(isc_ht_24_200000) ISC_TEST_ENTRY(isc_ht_24_200000)