From 05907d0d9e7c85f33e168feab1eb36b464425054 Mon Sep 17 00:00:00 2001
From: Lorenzo Beretta <vc.net.loreb@gmail.com>
Date: Mon, 25 Oct 2021 14:06:00 +0200
Subject: [PATCH] chfn: flush stdout before reading stdin and fix uninitialized
 variable

Same problem as described in https://github.com/karelzak/util-linux/pull/1481

Signed-off-by: Lorenzo Beretta <vc.net.loreb@gmail.com>

---
 login-utils/chfn.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/login-utils/chfn.c b/login-utils/chfn.c
index 2508e14..ece5cdc 100644
--- a/login-utils/chfn.c
+++ b/login-utils/chfn.c
@@ -227,7 +227,7 @@ static char *ask_new_field(struct chfn_control *ctl, const char *question,
 			   char *def_val)
 {
 	int len;
-	char *buf;
+	char *buf = NULL; /* leave initialized to NULL or getline segfaults */
 #ifndef HAVE_LIBREADLINE
 	size_t dummy = 0;
 #endif
@@ -242,6 +242,7 @@ static char *ask_new_field(struct chfn_control *ctl, const char *question,
 		if ((buf = readline(" ")) == NULL)
 #else
 		putchar(' ');
+		fflush(stdout);
 		if (getline(&buf, &dummy, stdin) < 0)
 #endif
 			errx(EXIT_FAILURE, _("Aborted."));
-- 
2.27.0

