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