mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
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:
@@ -387,20 +387,28 @@ static int yes_no(char *question_str)
|
||||
ssize_t bytes_read;
|
||||
char *answer;
|
||||
size_t size;
|
||||
int rc;
|
||||
|
||||
size = 0;
|
||||
answer = NULL;
|
||||
while (1) {
|
||||
printf("%s (y/n): ", question_str);
|
||||
bytes_read = getline(&answer, &size, stdin);
|
||||
if (bytes_read < 0)
|
||||
return -1;
|
||||
if (answer[0] == 'y')
|
||||
return 0;
|
||||
if (answer[0] == 'n')
|
||||
return 1;
|
||||
if (bytes_read < 0) {
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
if (answer[0] == 'y') {
|
||||
rc = 0;
|
||||
break;
|
||||
}
|
||||
if (answer[0] == 'n') {
|
||||
rc = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(answer);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static char *fdasd_partition_type(char *dsname)
|
||||
|
||||
Reference in New Issue
Block a user