[PATCH] Properly handle short writes in sigint handlers

Subject: [PATCH] Properly handle short writes in sigint handlers

Date: Thu, 22 Dec 2011 15:15:48 -0500

To: notmuch@notmuchmail.org

Cc:

From: Austin Clements


Even if we don't care about errors from write(2), it's still necessary
to handle short writes in order to use write correctly.  Some versions
of glibc even mark write as warn_unused_result because of this, so our
previous usage would trigger compiler warnings.
---
 notmuch-new.c |    5 ++++-
 notmuch-tag.c |    6 +++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index 3512de7..fc09bbb 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -66,8 +66,11 @@ static void
 handle_sigint (unused (int sig))
 {
     static char msg[] = "Stopping...         \n";
+    const char *pos = msg, *end = msg + sizeof (msg) - 1;
+    ssize_t c = 0;
 
-    (void) write(2, msg, sizeof(msg)-1);
+    for (; pos < end && c >= 0; pos += c)
+	c = write (2, pos, end - pos);
     interrupted = 1;
 }
 
diff --git a/notmuch-tag.c b/notmuch-tag.c
index 292c5da..0d4873d 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -26,7 +26,11 @@ static void
 handle_sigint (unused (int sig))
 {
     static char msg[] = "Stopping...         \n";
-    (void) write(2, msg, sizeof(msg)-1);
+    const char *pos = msg, *end = msg + sizeof (msg) - 1;
+    ssize_t c = 0;
+
+    for (; pos < end && c >= 0; pos += c)
+	c = write (2, pos, end - pos);
     interrupted = 1;
 }
 
-- 
1.7.7.3


Thread: