hashing: always use signed chars
authorRyan Lortie <desrt@desrt.ca>
Mon, 12 Sep 2011 12:06:13 +0000 (08:06 -0400)
committerRyan Lortie <desrt@desrt.ca>
Mon, 12 Sep 2011 12:12:44 +0000 (08:12 -0400)
Our hashing of non-ASCII strings was undefined due to the fact that
'char' is signed on some platforms, unsigned on others.  Always use a
signed char.

Discovered by Alexander Larsson.

https://bugzilla.gnome.org/show_bug.cgi?id=658806

gvdb-builder.c
gvdb-reader.c

index f65ca7d..91adec6 100644 (file)
@@ -93,7 +93,7 @@ djb_hash (const gchar *key)
   guint32 hash_value = 5381;
 
   while (*key)
-    hash_value = hash_value * 33 + *key++;
+    hash_value = hash_value * 33 + *(signed char *)key++;
 
   return hash_value;
 }
index 73f4f13..57816af 100644 (file)
@@ -254,7 +254,7 @@ gvdb_table_lookup (GvdbTable   *file,
     return NULL;
 
   for (key_length = 0; key[key_length]; key_length++)
-    hash_value = (hash_value * 33) + key[key_length];
+    hash_value = (hash_value * 33) + ((signed char *) key)[key_length];
 
   if (!gvdb_table_bloom_filter (file, hash_value))
     return NULL;