From: Haofeng Li When realloc() fails in transhuge-stress test, the original code exits immediately without freeing the previously allocated memory, causing a memory leak. This patch introduces a temporary pointer to hold the realloc result, ensuring proper cleanup by freeing the original map before exiting on allocation failure. Signed-off-by: Haofeng Li --- tools/testing/selftests/mm/transhuge-stress.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/mm/transhuge-stress.c b/tools/testing/selftests/mm/transhuge-stress.c index 68201192e37c..cbe86c5b8de0 100644 --- a/tools/testing/selftests/mm/transhuge-stress.c +++ b/tools/testing/selftests/mm/transhuge-stress.c @@ -30,7 +30,7 @@ int main(int argc, char **argv) int i = 0; char *name = NULL; double s; - uint8_t *map; + uint8_t *map, *map_tmp; size_t map_len; int pagemap_fd; int duration = 0; @@ -107,9 +107,12 @@ int main(int argc, char **argv) nr_succeed++; if (idx >= map_len) { - map = realloc(map, idx + 1); - if (!map) + map_tmp = realloc(map, idx + 1); + if (!map_tmp) { + free(map); ksft_exit_fail_msg("map realloc\n"); + } + map = map_tmp; memset(map + map_len, 0, idx + 1 - map_len); map_len = idx + 1; } -- 2.25.1