[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

hash api is clumsy

From: Jim Blandy <jimb_at_savonarola.red-bean.com>
Date: 2000-10-05 02:20:12 CEST

Greg, I've found an annoying quirk in the APR hash table interface.
Would it be possible to get this changed?

The function to add an entry to an APR hash table takes the key as a
pointer to raw bytes, and a byte count. If the byte count is zero,
then the length is assumed to be strlen (bytes) --- that is, the key
is assumed to be a standard C null-terminated string. I can certainly
see that this would be handy.

The problem is, I'm storing and retrieving data in hash tables where
the key is arbitrary binary data, whose length isn't under my control.
So I have to write every call to apr_hash_{set,get} like this:

      apr_hash_set (hash,
                    key_len > 0 ? key_data : "",
                    key_len, value);

Otherwise, if key_len happens to be zero --- i.e., the key is the
empty string --- APR will try to apply strlen to key_data, which
points to zero bytes of valid data. If I'm lucky, I get a segfault.
If I'm unlucky, strlen traverses some number of garbage bytes and
finds a null byte, and apr_hash_set stores my value under some garbage
key.

It would be nicer if apr_hash_{set,get} used a length of -1 to
indicate that the key is is a null-terminated string. That's a
nonsensical length, whereas zero is something that does come up.
Received on Sat Oct 21 14:36:10 2006

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.