bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] hash: once again explicitly disallow insertion of NULL


From: Jim Meyering
Subject: [PATCH] hash: once again explicitly disallow insertion of NULL
Date: Sun, 04 Jul 2010 11:13:07 +0200

I looked at allowing NULL key values in hash.c,
but decided not to pursue that right now.  The change below
reinstates the NULL check in hash_insert0 that I removed
a few days ago.

If you have to insert a NULL pointer, the hash module API has
a fundamental limitation.  For example, hash_lookup returns NULL to
indicate "not found", and hash_find_entry uses "bucket->data == NULL"
to indicate an empty bucket.

It'd be easy to document that hash_lookup doesn't work for such
data (and to provide hash_lookup0 that works), and probably feasible
to write a bucket_is_empty predicate to encapsulate a new condition,
but I have no need right now.

I've pushed this:

>From fbc47512f3f8e1533a28b4e3eca67938a2f9ebe7 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 4 Jul 2010 10:54:38 +0200
Subject: [PATCH] hash: once again explicitly disallow insertion of NULL

* lib/hash.c (hash_insert0): Reinstate just-removed test:
inserting a NULL pointer cannot work with these functions.
Add a comment with details.
This reverts part of the 2010-07-01 commit, 5bef1a35
"hash: extend module to deal with non-pointer keys".
---
 ChangeLog  |    9 +++++++++
 lib/hash.c |    9 ++++++++-
 2 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 61d0f53..f64cc8e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2010-07-04  Jim Meyering  <address@hidden>
+
+       hash: once again explicitly disallow insertion of NULL
+       * lib/hash.c (hash_insert0): Reinstate just-removed test:
+       inserting a NULL pointer cannot work with these functions.
+       Add a comment with details.
+       This reverts part of the 2010-07-01 commit, 5bef1a35
+       "hash: extend module to deal with non-pointer keys".
+
 2010-07-01  Bruno Haible  <address@hidden>

        stdbool: Update doc.
diff --git a/lib/hash.c b/lib/hash.c
index 4c359a4..15630be 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -1032,13 +1032,20 @@ hash_rehash (Hash_table *table, size_t candidate)
    hash_insert, the only way to distinguish those cases is to compare
    the return value and ENTRY.  That works only when you can have two
    different ENTRY values that point to data that compares "equal".  Thus,
-   when the ENTRY value is a simple scalar, you must use hash_insert0.  */
+   when the ENTRY value is a simple scalar, you must use hash_insert0.
+   ENTRY must not be NULL.  */
 int
 hash_insert0 (Hash_table *table, void const *entry, void const **matched_ent)
 {
   void *data;
   struct hash_entry *bucket;

+  /* The caller cannot insert a NULL entry, since hash_lookup returns NULL
+     to indicate "not found", and hash_find_entry uses "bucket->data == NULL"
+     to indicate an empty bucket.  */
+  if (! entry)
+    abort ();
+
   /* If there's a matching entry already in the table, return that.  */
   if ((data = hash_find_entry (table, entry, &bucket, false)) != NULL)
     {
--
1.7.2.rc1.192.g262ff



reply via email to

[Prev in Thread] Current Thread [Next in Thread]