Skip to content

Commit eb7261f

Browse files
Manciukicacmel
authored andcommitted
perf test: Add free() calls for scandir() returned dirent entries
ASan reported a memory leak for items of the entlist returned from scandir(). In fact, scandir() returns a malloc'd array of malloc'd dirents. This patch adds the missing (z)frees. Fixes: da96383 ("perf test: Iterate over shell tests in alphabetical order") Signed-off-by: Riccardo Mancini <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Fabian Hemmer <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Remi Bernon <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent afd4ad0 commit eb7261f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

tools/perf/tests/builtin-test.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/kernel.h>
2727
#include <linux/string.h>
2828
#include <subcmd/exec-cmd.h>
29+
#include <linux/zalloc.h>
2930

3031
static bool dont_fork;
3132

@@ -540,7 +541,7 @@ static int shell_tests__max_desc_width(void)
540541
{
541542
struct dirent **entlist;
542543
struct dirent *ent;
543-
int n_dirs;
544+
int n_dirs, e;
544545
char path_dir[PATH_MAX];
545546
const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
546547
int width = 0;
@@ -564,8 +565,9 @@ static int shell_tests__max_desc_width(void)
564565
}
565566
}
566567

568+
for (e = 0; e < n_dirs; e++)
569+
zfree(&entlist[e]);
567570
free(entlist);
568-
569571
return width;
570572
}
571573

@@ -596,7 +598,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width)
596598
{
597599
struct dirent **entlist;
598600
struct dirent *ent;
599-
int n_dirs;
601+
int n_dirs, e;
600602
char path_dir[PATH_MAX];
601603
struct shell_test st = {
602604
.dir = shell_tests__dir(path_dir, sizeof(path_dir)),
@@ -629,6 +631,8 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width)
629631
test_and_print(&test, false, -1);
630632
}
631633

634+
for (e = 0; e < n_dirs; e++)
635+
zfree(&entlist[e]);
632636
free(entlist);
633637
return 0;
634638
}
@@ -730,7 +734,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i)
730734
{
731735
struct dirent **entlist;
732736
struct dirent *ent;
733-
int n_dirs;
737+
int n_dirs, e;
734738
char path_dir[PATH_MAX];
735739
const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
736740

@@ -752,8 +756,11 @@ static int perf_test__list_shell(int argc, const char **argv, int i)
752756
continue;
753757

754758
pr_info("%2d: %s\n", i, t.desc);
759+
755760
}
756761

762+
for (e = 0; e < n_dirs; e++)
763+
zfree(&entlist[e]);
757764
free(entlist);
758765
return 0;
759766
}

0 commit comments

Comments
 (0)