Skip to content

Commit 7586d11

Browse files
namhyungacmel
authored andcommitted
perf list: Fix memory leaks in print_tracepoint_events()
It should free entries (not only the array) filled by scandirat() after use. Reviewed-by: Ian Rogers <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent b9f82b5 commit 7586d11

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

tools/perf/util/print-events.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus
8383
if (sys_dirent->d_type != DT_DIR ||
8484
!strcmp(sys_dirent->d_name, ".") ||
8585
!strcmp(sys_dirent->d_name, ".."))
86-
continue;
86+
goto next_sys;
8787

8888
dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH);
8989
if (dir_fd < 0)
90-
continue;
90+
goto next_sys;
9191

9292
evt_items = scandirat(events_fd, sys_dirent->d_name, &evt_namelist, NULL, alphasort);
9393
for (int j = 0; j < evt_items; j++) {
@@ -98,12 +98,12 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus
9898
if (evt_dirent->d_type != DT_DIR ||
9999
!strcmp(evt_dirent->d_name, ".") ||
100100
!strcmp(evt_dirent->d_name, ".."))
101-
continue;
101+
goto next_evt;
102102

103103
snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name);
104104
evt_fd = openat(dir_fd, evt_path, O_RDONLY);
105105
if (evt_fd < 0)
106-
continue;
106+
goto next_evt;
107107
close(evt_fd);
108108

109109
snprintf(evt_path, MAXPATHLEN, "%s:%s",
@@ -119,9 +119,13 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus
119119
/*desc=*/NULL,
120120
/*long_desc=*/NULL,
121121
/*encoding_desc=*/NULL);
122+
next_evt:
123+
free(evt_namelist[j]);
122124
}
123125
close(dir_fd);
124126
free(evt_namelist);
127+
next_sys:
128+
free(sys_namelist[i]);
125129
}
126130

127131
free(sys_namelist);

0 commit comments

Comments
 (0)