Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental allow to insert NULL values in the hashtable #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/sdb/ht_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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
#define HT_(name) HtPU##name
#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"
Expand Down
24 changes: 14 additions & 10 deletions src/ht.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* sdb - MIT - Copyright 2011-2022 - pancake */
/* sdb - MIT - Copyright 2011-2023 - pancake */

#include "sdb/ht.h"

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/ht_uu.c
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 4 additions & 0 deletions test/unit/test_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down