From: Federico Kircheis Date: Tue, 1 May 2018 14:55:20 +0000 (+0200) Subject: Avoid UB when clearing stdin X-Git-Tag: 2019-10-25~152 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=008987dffc6a7c55333a16afbc36c130fade6c68;p=neomutt Avoid UB when clearing stdin Flushing `stdin` is undefined behaviour. References http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf, chapter 7.21.5.2 (The fflush function) http://en.cppreference.com/w/c/io/fflush --- diff --git a/curs_lib.c b/curs_lib.c index fc045a5fb..921e5aa30 100644 --- a/curs_lib.c +++ b/curs_lib.c @@ -523,6 +523,19 @@ void mutt_perror_debug(const char *s) mutt_error("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno); } +/** + * mutt_flush_stdin - remove characters from stdin until '\n' or EOF is encountered + */ +void mutt_flush_stdin(void) +{ + int c; + do + { + c = fgetc(stdin); + } while ((c != '\n') && (c != EOF)); +} + + /** * mutt_any_key_to_continue - Prompt the user to 'press any key' and wait * @param s Message prompt @@ -550,7 +563,7 @@ int mutt_any_key_to_continue(const char *s) fputs(_("Press any key to continue..."), stdout); fflush(stdout); int ch = fgetc(stdin); - fflush(stdin); + mutt_flush_stdin(); tcsetattr(fd, TCSADRAIN, &old); close(fd); fputs("\r\n", stdout);