diff --git a/include/sdb/ht_inc.h b/include/sdb/ht_inc.h index d019d210..67d146c9 100644 --- a/include/sdb/ht_inc.h +++ b/include/sdb/ht_inc.h @@ -39,7 +39,7 @@ extern "C" { #define KEY_TYPE ut64 #define VALUE_TYPE ut64 #define KEY_TO_HASH(x) ((ut32)(x)) -#define HT_NULL_VALUE 0 +#define HT_NULL_VALUE UT64_MAX #else #define HtName_(name) name##PU #define Ht_(name) ht_pu_##name @@ -47,7 +47,7 @@ extern "C" { #define KEY_TYPE void * #define VALUE_TYPE ut64 #define KEY_TO_HASH(x) ((ut32)(uintptr_t)(x)) -#define HT_NULL_VALUE 0 +#define HT_NULL_VALUE UT64_MAX #endif #include "ls.h" diff --git a/src/ht.c b/src/ht.c index 45fffc6f..ece07912 100644 --- a/src/ht.c +++ b/src/ht.c @@ -1,4 +1,4 @@ -/* sdb - MIT - Copyright 2011-2022 - pancake */ +/* sdb - MIT - Copyright 2011-2023 - pancake */ #include "sdb/ht.h" @@ -16,20 +16,24 @@ SDB_API HtPP* sdb_ht_new(void) { } static bool sdb_ht_internal_insert(HtPP* ht, const char* key, const char* value, bool update) { - if (!ht || !key || !*key || !value) { + if (!ht) { // should be an assert return false; } SdbKv kvp = {{ 0 }}; - kvp.base.key = sdb_strdup (key); - if (!kvp.base.key) { - goto err; + if (key) { + kvp.base.key = sdb_strdup (key); + if (!kvp.base.key) { + goto err; + } + kvp.base.key_len = strlen ((const char *)kvp.base.key); } - kvp.base.value = sdb_strdup (value); - if (!kvp.base.value) { - goto err; + if (value) { + kvp.base.value = value? sdb_strdup (value): NULL; + if (!kvp.base.value) { + goto err; + } + kvp.base.value_len = strlen ((const char *)kvp.base.value); } - kvp.base.key_len = strlen ((const char *)kvp.base.key); - kvp.base.value_len = strlen ((const char *)kvp.base.value); kvp.expire = 0; return ht_pp_insert_kv (ht, (HtPPKv*)&kvp, update); diff --git a/src/ht_uu.c b/src/ht_uu.c index 28e7d936..40cf16a8 100644 --- a/src/ht_uu.c +++ b/src/ht_uu.c @@ -1,4 +1,4 @@ -/* sdb - MIT - Copyright 2018-2022 - ret2libc, pancake */ +/* sdb - MIT - Copyright 2018-2023 - ret2libc, pancake */ #include "sdb/sdb.h" #include "sdb/ht_uu.h" diff --git a/test/unit/test_hash.c b/test/unit/test_hash.c index f2a5e049..24c81641 100644 --- a/test/unit/test_hash.c +++ b/test/unit/test_hash.c @@ -11,10 +11,14 @@ typedef struct _test_struct { bool test_ht_insert_lookup(void) { HtPP *ht = sdb_ht_new (); + sdb_ht_insert (ht, "NULL", NULL); sdb_ht_insert (ht, "AAAA", "vAAAA"); sdb_ht_insert (ht, "BBBB", "vBBBB"); sdb_ht_insert (ht, "CCCC", "vCCCC"); + mu_assert_eq (sdb_ht_exists (ht, "NULL"), 1, "NULL value wrong"); + mu_assert_eq (sdb_ht_find (ht, "NULL", NULL), NULL, "NULL value wrong"); + mu_assert_eq (sdb_ht_find (ht, "NONE", NULL), NULL, "NULL value wrong"); mu_assert_streq (sdb_ht_find (ht, "BBBB", NULL), "vBBBB", "BBBB value wrong"); mu_assert_streq (sdb_ht_find (ht, "AAAA", NULL), "vAAAA", "AAAA value wrong"); mu_assert_streq (sdb_ht_find (ht, "CCCC", NULL), "vCCCC", "CCCC value wrong");