]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix case insensitive matching in isc_ht hash table implementation
authorOndřej Surý <ondrej@isc.org>
Sat, 10 Feb 2024 23:49:32 +0000 (00:49 +0100)
committerOndřej Surý <ondrej@isc.org>
Sun, 11 Feb 2024 08:36:56 +0000 (09:36 +0100)
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.

lib/isc/ht.c
tests/isc/ht_test.c

index f602e7195acfb529c052bbb3caae96b9d75f530d..dd49b007da803af9eafb070c944003ebe7e3659f 100644 (file)
@@ -14,6 +14,7 @@
 #include <inttypes.h>
 #include <string.h>
 
+#include <isc/ascii.h>
 #include <isc/hash.h>
 #include <isc/ht.h>
 #include <isc/magic.h>
@@ -95,9 +96,11 @@ isc__ht_iter_next(isc_ht_iter_t *it);
 
 static bool
 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 &&
-               memcmp(node->key, key, keysize) == 0);
+               (case_sensitive
+                        ? (memcmp(node->key, key, keysize) == 0)
+                        : (isc_ascii_lowerequal(node->key, key, keysize))));
 }
 
 static uint32_t
@@ -338,7 +341,9 @@ nexttable:
        for (isc_ht_node_t *node = ht->table[findex][hash]; node != NULL;
             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);
                }
        }
@@ -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;
             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) {
                                ht->table[idx][hash] = node->next;
                        } else {
index 0b90cbaa198f9ce0df79323ed3239bb577c38d61..2303def0089c48b76cd21df67ae3cbd1dca130b6 100644 (file)
@@ -330,7 +330,57 @@ ISC_RUN_TEST_IMPL(isc_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_ENTRY(isc_ht_case)
 ISC_TEST_ENTRY(isc_ht_1_120)
 ISC_TEST_ENTRY(isc_ht_6_1000)
 ISC_TEST_ENTRY(isc_ht_24_200000)