[cairo] cairo crash in _cairo_hash_string

Bill Spitzak spitzak at d2.com
Wed Aug 17 11:46:33 PDT 2005

Mitch wrote:

>   {
>       /* This is the djb2 hash. */
>       unsigned long hash = 5381;
> !     while (c && *c)
>         hash = ((hash << 5) + hash) + *c++;
>       return hash;
>   }

You may want null and zero-length strings to hash differently. If so the
following should work (null hashes as a string containing a single nul, 
and thus differently than any nul-terminated string):

	if (c) {
	  while (*c)
	    hash = ((hash << 5) + hash) + *c++;
	} else {
	  hash = (hash<<5) + hash;

I have also found cases where I want to make sure the hash is different 
for any possible string, so that the resulting hash can reflect whether 
or not this call is done. The solution I have done is to hash the 
zero-length string as a nul and the null pointer as nul,nul.

More information about the cairo mailing list