fdasd: Fix memory leak in yes_no() function

The yes_no() function was leaking memory when returning
early from the loop, as the 'answer' buffer allocated by
getline() was not freed before the return statements.

Restructure the function to use a single exit point, ensuring
free(answer) is always called before returning.

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Volkan Unal <vunal@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Volkan Unal
2026-06-26 20:53:32 +01:00
committed by Jan Höppner
parent 9a5886c0b3
commit 4ca93aa808

View File

@@ -387,20 +387,28 @@ static int yes_no(char *question_str)
ssize_t bytes_read; ssize_t bytes_read;
char *answer; char *answer;
size_t size; size_t size;
int rc;
size = 0; size = 0;
answer = NULL; answer = NULL;
while (1) { while (1) {
printf("%s (y/n): ", question_str); printf("%s (y/n): ", question_str);
bytes_read = getline(&answer, &size, stdin); bytes_read = getline(&answer, &size, stdin);
if (bytes_read < 0) if (bytes_read < 0) {
return -1; rc = -1;
if (answer[0] == 'y') break;
return 0; }
if (answer[0] == 'n') if (answer[0] == 'y') {
return 1; rc = 0;
break;
}
if (answer[0] == 'n') {
rc = 1;
break;
}
} }
free(answer); free(answer);
return rc;
} }
static char *fdasd_partition_type(char *dsname) static char *fdasd_partition_type(char *dsname)