From ffe94a73a147c6ef5086530734772bccfff6e70c Mon Sep 17 00:00:00 2001 From: Andrew Price Date: Apr 18 2017 12:56:06 +0000 Subject: mkfs.gfs2: Tidy up are_you_sure() Coverity pointed out that the 'line' pointer was dereferenced before it was NULL-checked and although that wasn't a bug it highlighted this function's strange structure. This patch tidies it up somewhat, keeping in mind that the memory allocated by getline() always has to be freed whether it succeeds or not. Signed-off-by: Andrew Price --- diff --git a/gfs2/mkfs/main_mkfs.c b/gfs2/mkfs/main_mkfs.c index 4436f93..7ede5ad 100644 --- a/gfs2/mkfs/main_mkfs.c +++ b/gfs2/mkfs/main_mkfs.c @@ -422,34 +422,29 @@ static void test_locking(struct mkfs_opts *opts) static void are_you_sure(void) { - char *line = NULL; - size_t len = 0; - int ret = -1; - int res = 0; + while (1) { + char *line = NULL; + size_t len = 0; + int ret; + int res; - do{ /* Translators: We use rpmatch(3) to match the answers to y/n questions in the user's own language, so the [y/n] here must also be translated to match one of the letters in the pattern printed by `locale -k yesexpr` and one of the letters in the pattern printed by `locale -k noexpr` */ - printf( _("Are you sure you want to proceed? [y/n]")); + printf( _("Are you sure you want to proceed? [y/n] ")); ret = getline(&line, &len, stdin); res = rpmatch(line); - - if (res > 0){ - free(line); - return; - } - if (!res){ - printf("\n"); - die( _("Aborted.\n")); - } - - }while(ret >= 0); - - if(line) free(line); + if (ret <= 0) + continue; + if (res == 1) /* Yes */ + return; + if (res == 0) /* No */ + exit(1); + /* Unrecognized input; go again. */ + }; } static unsigned choose_blocksize(struct mkfs_opts *opts)