Blob Blame Raw
From f1a97618031701ee9561e95ace4c7a52fd5dbd79 Mon Sep 17 00:00:00 2001
From: "Dmitry V. Levin" <ldv@altlinux.org>
Date: Thu, 18 May 2017 12:28:26 +0300
Subject: [PATCH] quotacheck: fix ask_yn UB when fgets returns NULL
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Do not use a random value from the stack as an answer when fgets
returns NULL, return the default value in the latter case.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Petr Písař <ppisar@redhat.com>
---
 quotacheck.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/quotacheck.c b/quotacheck.c
index 1675de8..689ceb9 100644
--- a/quotacheck.c
+++ b/quotacheck.c
@@ -600,8 +600,7 @@ int ask_yn(char *q, int def)
 
 	printf("%s [%c]: ", q, def ? 'y' : 'n');
 	fflush(stdout);
-	while (1) {
-		fgets(a, sizeof(a)-1, stdin);
+	while (fgets(a, sizeof(a)-1, stdin)) {
 		if (a[0] == '\n')
 			return def;
 		if (!strcasecmp(a, "y\n"))
@@ -611,6 +610,7 @@ int ask_yn(char *q, int def)
 		printf("Illegal answer. Please answer y/n: ");
 		fflush(stdout);
 	}
+	return def;
 }
 
 /* Do checks and buffer quota file into memory */
-- 
2.9.4