]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hashmap: add an explicit assert() for detecting when objects migrated between threads
authorLennart Poettering <lennart@poettering.net>
Fri, 3 Aug 2018 14:46:26 +0000 (16:46 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 3 Aug 2018 15:36:11 +0000 (17:36 +0200)
When clients don't follow protocol and use the same object from
different threads, then we previously would silently corrupt memory.
With this assert we'll fail with an assert(). This doesn't fix anything
but certainly makes mis-uses easier to detect and debug.

Triggered by https://bugzilla.redhat.com/show_bug.cgi?id=1609349

src/basic/hashmap.c

index 65e22ccd21feda6c60845c8a759b3a895a22e168..44d718c83d223b1c5abb11ed295e2ef967f3034f 100644 (file)
@@ -869,9 +869,11 @@ static void hashmap_free_no_clear(HashmapBase *h) {
         assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
 #endif
 
-        if (h->from_pool)
+        if (h->from_pool) {
+                /* Ensure that the object didn't get migrated between threads. */
+                assert_se(is_main_thread());
                 mempool_free_tile(hashmap_type_info[h->type].mempool, h);
-        else
+        else
                 free(h);
 }