--- /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 95160431FAF\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Jan 2013 06:05: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 xUF+PCnxewCZ for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Jan 2013 06:05:23 -0800 (PST)\r
+Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\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 EDC70431FAE\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Jan 2013 06:05:22 -0800 (PST)\r
+Received: from fctnnbsc30w-156034082078.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+ ([156.34.82.78] helo=zancas.localnet)\r
+ by tesseract.cs.unb.ca with esmtpsa\r
+ (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1Trqql-0005X8-4a; Sun, 06 Jan 2013 10:05:19 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1Trqqf-00088k-Gt; Sun, 06 Jan 2013 10:05:13 -0400\r
+From: david@tethera.net\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v2] notmuch-restore: handle empty input file,\r
+ leading blank lines and comments.\r
+Date: Sun, 6 Jan 2013 10:04:56 -0400\r
+Message-Id: <1357481096-31254-1-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <m2wqvri8k3.fsf@guru.guru-group.fi>\r
+References: <m2wqvri8k3.fsf@guru.guru-group.fi>\r
+X-Spam_bar: -\r
+Cc: David Bremner <bremner@debian.org>\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, 06 Jan 2013 14:05:23 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+This patch corrects several undesirable behaviours:\r
+\r
+1) Empty files were not detected, leading to buffer read overrun.\r
+\r
+2) An initial blank line cause restore to silently abort\r
+\r
+3) Initial comment line caused format detection to fail\r
+---\r
+ notmuch-restore.c | 18 +++++++++++++-----\r
+ test/dump-restore | 3 ---\r
+ 2 files changed, 13 insertions(+), 8 deletions(-)\r
+\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index d43546d..f436989 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -181,11 +181,6 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ argv[opt_index]);\r
+ return 1;\r
+ }\r
+- char *p;\r
+-\r
+- line_len = getline (&line, &line_size, input);\r
+- if (line_len == 0)\r
+- return 0;\r
+ \r
+ tag_ops = tag_op_list_create (ctx);\r
+ if (tag_ops == NULL) {\r
+@@ -193,6 +188,19 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ return 1;\r
+ }\r
+ \r
++ do {\r
++ line_len = getline (&line, &line_size, input);\r
++\r
++ /* empty input file not considered an error */\r
++ if (line_len < 0)\r
++ return 0;\r
++\r
++ } while ((line_len == 0) ||\r
++ (line[0] == '#') ||\r
++ /* the cast is safe because we checked about for line_len < 0 */\r
++ (strspn (line, " \t\n") == (unsigned)line_len));\r
++\r
++ char *p;\r
+ for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {\r
+ if (*p == '(')\r
+ input_format = DUMP_FORMAT_SUP;\r
+diff --git a/test/dump-restore b/test/dump-restore\r
+index c2ddb92..ae30cd1 100755\r
+--- a/test/dump-restore\r
++++ b/test/dump-restore\r
+@@ -146,13 +146,11 @@ cat <<EOF > comments-and-blanks\r
+ EOF\r
+ \r
+ test_begin_subtest 'restoring empty file is not an error'\r
+-test_subtest_known_broken\r
+ notmuch restore < /dev/null 2>OUTPUT.$test_count\r
+ cp /dev/null EXPECTED\r
+ test_expect_equal_file EXPECTED OUTPUT.$test_count\r
+ \r
+ test_begin_subtest 'file of comments and blank lines is not an error'\r
+-test_subtest_known_broken\r
+ notmuch restore --input=comments-and-blanks\r
+ ret_val=$?\r
+ test_expect_equal "$ret_val" "0"\r
+@@ -172,7 +170,6 @@ echo "yun1vjwegii.fsf@aiko.keithp.com (another_tag)" \\r
+ >> leading-comments-blanks-sup\r
+ \r
+ test_begin_subtest 'detect format=sup with leading comments and blanks'\r
+-test_subtest_known_broken\r
+ notmuch restore --input=leading-comments-blanks-sup\r
+ notmuch search --output=tags id:yun1vjwegii.fsf@aiko.keithp.com > OUTPUT.$test_count\r
+ echo "another_tag" > EXPECTED\r
+-- \r
+1.7.10.4\r
+\r