From 2e6941264d71169f710d78ec41a6112e19303373 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Sun, 25 Nov 2012 23:57:02 +1900 Subject: [PATCH] [PATCH 1/6] lib: Clean up error handling in _notmuch_thread_create --- c8/242a45ac469a1a1b1ade0564875513e0682c59 | 161 ++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 c8/242a45ac469a1a1b1ade0564875513e0682c59 diff --git a/c8/242a45ac469a1a1b1ade0564875513e0682c59 b/c8/242a45ac469a1a1b1ade0564875513e0682c59 new file mode 100644 index 000000000..db3e3c3f4 --- /dev/null +++ b/c8/242a45ac469a1a1b1ade0564875513e0682c59 @@ -0,0 +1,161 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 2EFAE431FAF + for ; Sat, 24 Nov 2012 20:57:20 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id JFE7F7CgxTJd for ; + Sat, 24 Nov 2012 20:57:19 -0800 (PST) +Received: from dmz-mailsec-scanner-6.mit.edu (DMZ-MAILSEC-SCANNER-6.MIT.EDU + [18.7.68.35]) + by olra.theworths.org (Postfix) with ESMTP id 2DF41431FAE + for ; Sat, 24 Nov 2012 20:57:19 -0800 (PST) +X-AuditID: 12074423-b7fab6d0000008f9-43-50b1a52e24ca +Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) + by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP + id F9.CE.02297.E25A1B05; Sat, 24 Nov 2012 23:57:18 -0500 (EST) +Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) + by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id qAP4vGJa012786; + Sat, 24 Nov 2012 23:57:16 -0500 +Received: from drake.dyndns.org + (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com + [209.6.116.242]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id qAP4vDSu000081 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Sat, 24 Nov 2012 23:57:16 -0500 (EST) +Received: from amthrax by drake.dyndns.org with local (Exim 4.77) + (envelope-from ) + id 1TcUHJ-0003Zl-Ro; Sat, 24 Nov 2012 23:57:13 -0500 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH 1/6] lib: Clean up error handling in _notmuch_thread_create +Date: Sat, 24 Nov 2012 23:57:02 -0500 +Message-Id: <1353819427-13182-2-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10.4 +In-Reply-To: <1353819427-13182-1-git-send-email-amdragon@mit.edu> +References: <1353819427-13182-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrNIsWRmVeSWpSXmKPExsUixG6nrqu3dGOAwe9jHBar5/JYXL85k9mB + yWPnrLvsHs9W3WIOYIrisklJzcksSy3St0vgyvg0/ThTwRGxiq8rdrE2MH4U7GLk5JAQMJF4 + ++UEC4QtJnHh3nq2LkYuDiGBfYwS6/79h3I2MEqsn9oC5TxikljWshrKmcsosejcWXaQfjYB + DYlt+5czgtgiAtISO+/OZgWxmQUcJT7vXwTUwMEhLOAtsbfTESTMIqAq8fznX7ByXgEHiddv + 3zNDnKEo0f1sAlg5J1Br4145kLAQUMmtf5OYJzDyL2BkWMUom5JbpZubmJlTnJqsW5ycmJeX + WqRrppebWaKXmlK6iREcRi7KOxj/HFQ6xCjAwajEw3sjcWOAEGtiWXFl7iFGSQ4mJVHezEVA + Ib6k/JTKjMTijPii0pzU4kOMEhzMSiK81qpAOd6UxMqq1KJ8mJQ0B4uSOO+1lJv+QgLpiSWp + 2ampBalFMFkZDg4lCV6RJUCNgkWp6akVaZk5JQhpJg5OkOE8QMNFQWp4iwsSc4sz0yHypxgV + pcR5zUESAiCJjNI8uF5YnL9iFAd6RZiXCaSKB5gi4LpfAQ1mAhr8dPY6kMEliQgpqQbG7E1e + fEvtj5hc/NN+uHtrvfvO5UvWPbCNM3vrPCsyv+OdzuNAj1fhR6SzMm/m3VJ3/vRK0MjeUD86 + p8u1hHHy8dC5kRyW7HVPbIxNBHTMWlpDNxyw+KrX3iBzgkXygcHbx4cyTtme1FxVEtok9HyP + XtOezuCl8rsvf/k3/ajC+aeK55v+R9xUYinOSDTUYi4qTgQA52uHhM4CAAA= +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sun, 25 Nov 2012 04:57:20 -0000 + +Previously, there were various opportunities for memory leaks in the +error-handling paths of this function. Use a local talloc context and +some reparenting to make eliminate these leaks, while keeping the +control flow simple. +--- + lib/thread.cc | 33 +++++++++++++++++++-------------- + 1 file changed, 19 insertions(+), 14 deletions(-) + +diff --git a/lib/thread.cc b/lib/thread.cc +index e976d64..aed87b1 100644 +--- a/lib/thread.cc ++++ b/lib/thread.cc +@@ -406,7 +406,8 @@ _notmuch_thread_create (void *ctx, + notmuch_string_list_t *exclude_terms, + notmuch_sort_t sort) + { +- notmuch_thread_t *thread; ++ void *local = talloc_new (ctx); ++ notmuch_thread_t *thread = NULL; + notmuch_message_t *seed_message; + const char *thread_id; + char *thread_id_query_string; +@@ -415,24 +416,23 @@ _notmuch_thread_create (void *ctx, + notmuch_messages_t *messages; + notmuch_message_t *message; + +- seed_message = _notmuch_message_create (ctx, notmuch, seed_doc_id, NULL); ++ seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL); + if (! seed_message) + INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id); + + thread_id = notmuch_message_get_thread_id (seed_message); +- thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id); ++ thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id); + if (unlikely (thread_id_query_string == NULL)) +- return NULL; ++ goto DONE; + +- thread_id_query = notmuch_query_create (notmuch, thread_id_query_string); ++ thread_id_query = talloc_steal ( ++ local, notmuch_query_create (notmuch, thread_id_query_string)); + if (unlikely (thread_id_query == NULL)) +- return NULL; ++ goto DONE; + +- talloc_free (thread_id_query_string); +- +- thread = talloc (ctx, notmuch_thread_t); ++ thread = talloc (local, notmuch_thread_t); + if (unlikely (thread == NULL)) +- return NULL; ++ goto DONE; + + talloc_set_destructor (thread, _notmuch_thread_destructor); + +@@ -451,8 +451,10 @@ _notmuch_thread_create (void *ctx, + free, NULL); + + thread->message_list = _notmuch_message_list_create (thread); +- if (unlikely (thread->message_list == NULL)) +- return NULL; ++ if (unlikely (thread->message_list == NULL)) { ++ thread = NULL; ++ goto DONE; ++ } + + thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal, + free, NULL); +@@ -489,12 +491,15 @@ _notmuch_thread_create (void *ctx, + _notmuch_message_close (message); + } + +- notmuch_query_destroy (thread_id_query); +- + _resolve_thread_authors_string (thread); + + _resolve_thread_relationships (thread); + ++ /* Commit to returning thread. */ ++ talloc_steal (ctx, thread); ++ ++ DONE: ++ talloc_free (local); + return thread; + } + +-- +1.7.10.4 + -- 2.26.2