]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
DBusHash: Program a bit more defensively
authorSimon McVittie <smcv@collabora.com>
Fri, 17 Aug 2018 18:50:13 +0000 (19:50 +0100)
committerSimon McVittie <smcv@collabora.com>
Mon, 3 Dec 2018 19:05:13 +0000 (19:05 +0000)
In particular, the assertions that bucket >= table->buckets and
bucket <= &table->buckets[table->n_buckets - 1] catch the bug fixed
by the previous commit, by ensuring that bucket is somewhere inside
the new array of buckets.

Signed-off-by: Simon McVittie <smcv@collabora.com>
dbus/dbus-hash.c

index b438a2e02ee6a7697393c6b7f954e88d38bfcb6b..a931976e30589c7a7cd86d2b77c2747ac37e53d2 100644 (file)
@@ -745,8 +745,8 @@ _dbus_hash_iter_lookup (DBusHashTable *table,
                         DBusHashIter  *iter)
 {
   DBusRealHashIter *real;
-  DBusHashEntry *entry;
-  DBusHashEntry **bucket;
+  DBusHashEntry *entry = NULL;
+  DBusHashEntry **bucket = NULL;
   
   _DBUS_STATIC_ASSERT (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
   
@@ -754,9 +754,15 @@ _dbus_hash_iter_lookup (DBusHashTable *table,
 
   entry = (* table->find_function) (table, key, create_if_not_found, &bucket, NULL);
 
+  /* entry == NULL means not found, and either !create_if_not_found or OOM */
   if (entry == NULL)
     return FALSE;
 
+  _dbus_assert (bucket != NULL);
+  _dbus_assert (table->n_buckets >= 1);
+  _dbus_assert (bucket >= table->buckets);
+  _dbus_assert (bucket <= &table->buckets[table->n_buckets - 1]);
+
   if (create_if_not_found)
     {
       if (table->free_key_function && entry->key != key)
@@ -772,6 +778,8 @@ _dbus_hash_iter_lookup (DBusHashTable *table,
   real->next_bucket = (bucket - table->buckets) + 1;
   real->n_entries_on_init = table->n_entries; 
 
+  _dbus_assert (real->next_bucket >= 0);
+  _dbus_assert (real->next_bucket <= table->n_buckets);
   _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
   
   return TRUE;
@@ -856,6 +864,7 @@ add_entry (DBusHashTable        *table,
     }
 
   add_allocated_entry (table, entry, idx, key, bucket);
+  _dbus_assert (bucket == NULL || *bucket != NULL);
 
   return entry;
 }
@@ -913,10 +922,19 @@ find_generic_function (DBusHashTable        *table,
     }
 
   if (create_if_not_found)
-    entry = add_entry (table, idx, key, bucket, preallocated);
+    {
+      entry = add_entry (table, idx, key, bucket, preallocated);
+
+      if (entry == NULL)  /* OOM */
+        return NULL;
+
+      _dbus_assert (bucket == NULL || *bucket != NULL);
+    }
   else if (preallocated)
-    _dbus_hash_table_free_preallocated_entry (table, preallocated);
-  
+    {
+      _dbus_hash_table_free_preallocated_entry (table, preallocated);
+    }
+
   return entry;
 }