mirror of
https://gitlab.freedesktop.org/dbus/dbus.git
synced 2026-05-09 04:58:02 +02:00
DBusHash: Program a bit more defensively
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>
This commit is contained in:
parent
451192ba8a
commit
f9700a8a47
1 changed files with 23 additions and 5 deletions
|
|
@ -745,8 +745,8 @@ _dbus_hash_iter_lookup (DBusHashTable *table,
|
||||||
DBusHashIter *iter)
|
DBusHashIter *iter)
|
||||||
{
|
{
|
||||||
DBusRealHashIter *real;
|
DBusRealHashIter *real;
|
||||||
DBusHashEntry *entry;
|
DBusHashEntry *entry = NULL;
|
||||||
DBusHashEntry **bucket;
|
DBusHashEntry **bucket = NULL;
|
||||||
|
|
||||||
_DBUS_STATIC_ASSERT (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
|
_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 = (* 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)
|
if (entry == NULL)
|
||||||
return FALSE;
|
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 (create_if_not_found)
|
||||||
{
|
{
|
||||||
if (table->free_key_function && entry->key != key)
|
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->next_bucket = (bucket - table->buckets) + 1;
|
||||||
real->n_entries_on_init = table->n_entries;
|
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);
|
_dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
@ -856,6 +864,7 @@ add_entry (DBusHashTable *table,
|
||||||
}
|
}
|
||||||
|
|
||||||
add_allocated_entry (table, entry, idx, key, bucket);
|
add_allocated_entry (table, entry, idx, key, bucket);
|
||||||
|
_dbus_assert (bucket == NULL || *bucket != NULL);
|
||||||
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
@ -913,10 +922,19 @@ find_generic_function (DBusHashTable *table,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (create_if_not_found)
|
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)
|
else if (preallocated)
|
||||||
_dbus_hash_table_free_preallocated_entry (table, preallocated);
|
{
|
||||||
|
_dbus_hash_table_free_preallocated_entry (table, preallocated);
|
||||||
|
}
|
||||||
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue