libmisc/yesno.c: Fix regression

The getline function does not return a pointer but the amount of read
characters. The error return value to check for is -1.

Set buf to NULL to avoid dereference of an uninitialized stack value.

The getline function returns -1 if size argument is NULL. Always use
a valid pointer even if size is unimportant.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
This commit is contained in:
Samanta Navarro 2023-04-28 11:54:38 +00:00 committed by Serge Hallyn
parent 0c4fa6ee0a
commit c0fc4d2122

View File

@ -50,8 +50,9 @@ static int rpmatch(const char *response);
bool bool
yes_or_no(bool read_only) yes_or_no(bool read_only)
{ {
bool ret; bool ret;
char *buf; char *buf;
size_t size;
if (read_only) { if (read_only) {
puts(_("No")); puts(_("No"));
@ -60,8 +61,10 @@ yes_or_no(bool read_only)
fflush(stdout); fflush(stdout);
buf = NULL;
ret = false; ret = false;
if (getline(&buf, NULL, stdin) != NULL) size = 0;
if (getline(&buf, &size, stdin) != -1)
ret = rpmatch(buf) == 1; ret = rpmatch(buf) == 1;
free(buf); free(buf);