diff --git a/src/t_hash.c b/src/t_hash.c index 6c3049cecec..f666caebf48 100644 --- a/src/t_hash.c +++ b/src/t_hash.c @@ -322,6 +322,7 @@ bool hashTypeHasStringRef(robj *o, sds field) { if (objectGetEncoding(o) == OBJ_ENCODING_LISTPACK) return false; hashtable *ht = objectGetVal(o); void **entry_ref = hashtableFindRef(ht, field); + if (!entry_ref) return false; return (entryHasStringRef(*entry_ref)); } diff --git a/tests/unit/moduleapi/hash_stringref.tcl b/tests/unit/moduleapi/hash_stringref.tcl index a2efb23cc8b..1c517c78671 100644 --- a/tests/unit/moduleapi/hash_stringref.tcl +++ b/tests/unit/moduleapi/hash_stringref.tcl @@ -14,6 +14,17 @@ start_server {tags {"modules"}} { assert_equal "1" [r hash.has_stringref k f] } + test {Module hash has_stringref on non-existent field} { + r del k + r hset k f hello1 + # Setting a stringref converts the hash to the hashtable encoding, which + # is the only encoding that looks the field up in the hashtable. + r hash.set_stringref k f hello1 + assert_encoding hashtable k + # Missing field must return 0 rather than dereferencing a NULL entry. + assert_equal "0" [r hash.has_stringref k nonexistent] + } + test "Unload the module - hash" { assert_equal {OK} [r module unload hash.stringref] }