unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Carl Worth <cworth@cworth.org>
To: djcb@djcbsoftware.nl,
	"notmuch\@notmuchmail org" <notmuch@notmuchmail.org>
Subject: Re: [PATCH 2/2] * avoid gcc 4.4.1 compiler warning due to ignored 'fflush' return value
Date: Tue, 01 Dec 2009 08:08:07 -0800	[thread overview]
Message-ID: <87k4x6ad94.fsf@yoom.home.cworth.org> (raw)
In-Reply-To: <87r5rpyd4x.wl%djcb@djcbsoftware.nl>

[-- Attachment #1: Type: text/plain, Size: 2657 bytes --]

On Mon, 23 Nov 2009 08:21:50 +0200, Dirk-Jan C. Binnema <djcb.bulk@gmail.com> wrote:
> -#define prompt(format, ...)				\
> -    do {						\
> -	printf (format, ##__VA_ARGS__);			\
> -	fflush (stdout);				\
> -	getline (&response, &response_size, stdin);	\
> -	chomp_newline (response);			\
> +#define prompt(format, ...)					\
> +    do {							\
> +	int ignored;						\
> +	printf (format, ##__VA_ARGS__);				\
> +	fflush (stdout);					\
> +	ignored = getline (&response, &response_size, stdin);	\
> +	chomp_newline (response);				\
>      } while (0)

This patch is incorrect. Ignoring the return value of getline results in
the program invoking undefined behavior by reading uninitialized
memory. This is easily tested by, for example, typing Control-D to
provide EOF to a prompt from "notmuch setup".

How about just exiting in case of EOF as in the patch below?

I think I'll push it now.

-Carl

commit eb0cf86c7a9d5cda464d4d36a9cac66f26b5529d
Author: Carl Worth <cworth@cworth.org>
Date:   Tue Dec 1 08:06:09 2009 -0800

    notmuch setup: Exit if EOF is encountered at any prompt.
    
    If the user is explicitly providing EOF, then terminating the program
    is the most likely desired thing to do. This also avoids undefined
    behavior from continuing with an uninitialized response after ignoring
    the return value of getline().

diff --git a/notmuch-setup.c b/notmuch-setup.c
index 5ec176d..622bbaa 100644
--- a/notmuch-setup.c
+++ b/notmuch-setup.c
@@ -100,12 +100,15 @@ notmuch_setup_command (unused (void *ctx),
     unsigned int i;
     int is_new;
 
-#define prompt(format, ...)                            \
-    do {                                               \
-       printf (format, ##__VA_ARGS__);                 \
-       fflush (stdout);                                \
-       getline (&response, &response_size, stdin);     \
-       chomp_newline (response);                       \
+#define prompt(format, ...)                                    \
+    do {                                                       \
+       printf (format, ##__VA_ARGS__);                         \
+       fflush (stdout);                                        \
+       if (getline (&response, &response_size, stdin) < 0) {   \
+           printf ("Exiting.\n");                              \
+           exit (1);                                           \
+       }                                                       \
+       chomp_newline (response);                               \
     } while (0)
 
     config = notmuch_config_open (ctx, NULL, &is_new);

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

  reply	other threads:[~2009-12-01 16:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-23  6:21 [PATCH 2/2] * avoid gcc 4.4.1 compiler warning due to ignored 'fflush' return value Dirk-Jan C. Binnema
2009-12-01 16:08 ` Carl Worth [this message]
2009-12-01 18:50   ` Dirk-Jan C. Binnema
2009-12-02  3:01     ` Carl Worth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k4x6ad94.fsf@yoom.home.cworth.org \
    --to=cworth@cworth.org \
    --cc=djcb@djcbsoftware.nl \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).