[uthash] Question regarding unique keys

59 views
Skip to first unread message

silvioprog

unread,
Mar 5, 2018, 11:40:51 PM3/5/18
to uth...@googlegroups.com
Hello dudes.

A quote is from uthash documentation:

"Unique keys
As with any hash, every item must have a unique key. Your application must enforce key uniqueness. Before you add an item to the hash table, you must first know (if in doubt, check!) that the key is not already in use. You can check whether a key already exists in the hash table using HASH_FIND."

I totally agree, but, what happens if I use duplicated keys anyway? I have a situation that I need to send two HTTP headers declaring the same name in both, they are distinguished only by their values. Then I purposely added some duplicated keys in a test and iterated it fine, all the names (duplicated) and values was printed fine.

So, in situations like this, could I use uthash without any risk?

Thank you!

--
Silvio Clécio

silvioprog

unread,
Mar 5, 2018, 11:47:44 PM3/5/18
to uth...@googlegroups.com
Oops...

On Tue, Mar 6, 2018 at 1:40 AM, silvioprog <silvi...@gmail.com> wrote:
[...]

A quote is from uthash documentation:

I meant "This quote ..." ^^'

--
Silvio Clécio

Arthur O'Dwyer

unread,
Mar 6, 2018, 12:12:33 AM3/6/18
to uth...@googlegroups.com
My understanding is that you could use uthash just fine with duplicated keys; but you should be aware that e.g. HASH_REPLACE and HASH_DELETE will not necessarily do what you expect. (In practice, I believe they'd remove the first duplicate and leave the others, if any. You could certainly look at the code and find out.)
"Officially," I suppose the answer is undefined behavior. The documentation says that if you enforce key uniqueness, uthash behaves in such-and-such a way. The documentation doesn't say anything about what happens if you don't enforce key uniqueness. So, whatever ends up happening, it won't be considered a bug. "Garbage in, garbage out."

Since no particular behavior is documented, it is possible (but IMHO unlikely) that future versions of uthash.h might change the implemented behavior.

–Arthur
Reply all
Reply to author
Forward
0 new messages