--- /dev/null
+Return-Path: <bremner@tethera.net>\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 207A2431FC2\r
+ for <notmuch@notmuchmail.org>; Wed, 22 Jan 2014 08:43:23 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ 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 Nn-n0HHyTOXP for <notmuch@notmuchmail.org>;\r
+ Wed, 22 Jan 2014 08:43:15 -0800 (PST)\r
+Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
+ (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id A7602431FAF\r
+ for <notmuch@notmuchmail.org>; Wed, 22 Jan 2014 08:43:15 -0800 (PST)\r
+Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1W60tU-0002Ls-K2; Wed, 22 Jan 2014 12:43:12 -0400\r
+Received: (nullmailer pid 28647 invoked by uid 1000); Wed, 22 Jan 2014\r
+ 16:43:08 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] lib: make notmuch_threads_valid return FALSE when passed NULL\r
+Date: Wed, 22 Jan 2014 12:42:59 -0400\r
+Message-Id: <1390408979-28606-1-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.8.5.2\r
+In-Reply-To: <8738kgrxyf.fsf@zancas.localnet>\r
+References: <8738kgrxyf.fsf@zancas.localnet>\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: Wed, 22 Jan 2014 16:43:23 -0000\r
+\r
+Without this patch, the example code in the header docs crashes for certain\r
+invalid queries (see id:871u00oimv.fsf@approx.mit.edu)\r
+---\r
+ lib/notmuch.h | 2 ++\r
+ lib/query.cc | 3 +++\r
+ 2 files changed, 5 insertions(+)\r
+\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 02604c5..68896ae 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -802,6 +802,8 @@ notmuch_query_destroy (notmuch_query_t *query);\r
+ * valid object. Whereas when this function returns FALSE,\r
+ * notmuch_threads_get will return NULL.\r
+ *\r
++ * If passed a NULL pointer, this function returns FALSE\r
++ *\r
+ * See the documentation of notmuch_query_search_threads for example\r
+ * code showing how to iterate over a notmuch_threads_t object.\r
+ */\r
+diff --git a/lib/query.cc b/lib/query.cc\r
+index ec60e2e..60ff8bd 100644\r
+--- a/lib/query.cc\r
++++ b/lib/query.cc\r
+@@ -462,6 +462,9 @@ notmuch_threads_valid (notmuch_threads_t *threads)\r
+ {\r
+ unsigned int doc_id;\r
+ \r
++ if (! threads)\r
++ return FALSE;\r
++\r
+ while (threads->doc_id_pos < threads->doc_ids->len) {\r
+ doc_id = g_array_index (threads->doc_ids, unsigned int,\r
+ threads->doc_id_pos);\r
+-- \r
+1.8.5.2\r
+\r