]> git.notmuchmail.org Git - notmuch/commitdiff
lib: fix g_hash_table related read-after-free bug
authorDavid Bremner <david@tethera.net>
Wed, 22 Feb 2017 10:17:47 +0000 (06:17 -0400)
committerDavid Bremner <david@tethera.net>
Wed, 22 Feb 2017 10:28:03 +0000 (06:28 -0400)
The two g_hash_table functions (insert, add) have different behaviour
with respect to existing keys. g_hash_table_insert frees the new key,
while g_hash_table_add (which is really g_hash_table_replace in
disguise) frees the existing key. With this change 'ref' is live until
the end of the function (assuming single-threaded access to
'hash'). We can't guarantee it will continue to be live in the
future (i.e. there may be a future key duplication) so we copy it with
the allocation context passed to parse_references (in practice this is
the notmuch_message_t object whose parents we are finding).

Thanks to Tomi for the simpler approach to the problem based on
reading the fine glib manual.

lib/database.cc

index f0bfe5667fba4736996583830047d7529cd390fc..eddb780c69ebc76ab8870fb93dfb23d01e1a6176 100644 (file)
@@ -652,7 +652,7 @@ parse_references (void *ctx,
        ref = _parse_message_id (ctx, refs, &refs);
 
        if (ref && strcmp (ref, message_id)) {
-           g_hash_table_insert (hash, ref, NULL);
+           g_hash_table_add (hash, ref);
            last_ref = ref;
        }
     }
@@ -661,7 +661,7 @@ parse_references (void *ctx,
      * reference to the database.  We should avoid making a message
      * its own parent, thus the above check.
      */
-    return last_ref;
+    return talloc_strdup(ctx, last_ref);
 }
 
 notmuch_status_t