btrfs-progs: restore: Fix input buffer handling

fgets consumes n-1 bytes from input buffer.

When a user types y\n, the newline is left in the buffer.  As a result,
the next fgets uses that \n as answer without waiting for the user to
type.

This patch also fix a bug that dereference the ret without checking if
it's NULL.

* Consumes the `\n` from stdin buffer
* Avoid NULL pointer dereference: treat EOF as default value

Pull-request: #182
Author: pjw91 <mail6543210@yahoo.com.tw>
Signed-off-by: David Sterba <dsterba@suse.com>
master
pjw91 2019-06-19 02:24:53 +08:00 committed by David Sterba
parent 5dfbc7c42a
commit 064341dca9
1 changed files with 3 additions and 3 deletions

View File

@ -488,14 +488,14 @@ enum loop_response {
static enum loop_response ask_to_continue(const char *file)
{
char buf[2];
char buf[16];
char *ret;
printf("We seem to be looping a lot on %s, do you want to keep going "
"on ? (y/N/a): ", file);
again:
ret = fgets(buf, 2, stdin);
if (*ret == '\n' || tolower(*ret) == 'n')
ret = fgets(buf, 16, stdin);
if (!ret || *ret == '\n' || tolower(*ret) == 'n')
return LOOP_STOP;
if (tolower(*ret) == 'a')
return LOOP_DONTASK;