perf list: Fix memory leaks in print_tracepoint_events()
Namhyung Kim
maintainerPATCH 1/2 perf list: Fix memory leaks in print_tracepoint_events()
It should free entries (not only the array) filled by scandirat() after use.
--- tools/perf/util/print-events.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
tools/perf/util/print-events.c +8 -4
@@ -83,11 +83,11 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unusif (sys_dirent->d_type !=DT_DIR || !strcmp (sys_dirent->d_name,"." ) || !strcmp (sys_dirent->d_name,".." )) -continue ; + goto next_sys; dir_fd =openat (events_fd, sys_dirent->d_name,O_PATH );if (dir_fd <0 ) -continue ; + goto next_sys; evt_items =scandirat (events_fd, sys_dirent->d_name, &evt_namelist,NULL , alphasort);for (int j =0 ; j < evt_items; j++) {
@@ -98,12 +98,12 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unusif (evt_dirent->d_type !=DT_DIR || !strcmp (evt_dirent->d_name,"." ) || !strcmp (evt_dirent->d_name,".." )) -continue ; + goto next_evt;snprintf (evt_path,sizeof (evt_path),"%s/id" , evt_dirent->d_name); evt_fd =openat (dir_fd, evt_path,O_RDONLY );if (evt_fd <0 ) -continue ; + goto next_evt;close (evt_fd); snprintf(evt_path, MAXPATHLEN, "%s:%s",
@@ -119,9 +119,13 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus/*desc=*/ NULL ,/*long_desc=*/ NULL , /*encoding_desc=*/NULL); +next_evt: +free (evt_namelist[j]); }close (dir_fd);free (evt_namelist); +next_sys: +free (sys_namelist[i]); }free (sys_namelist);
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
Namhyung Kim
maintainerPATCH 2/2 perf list: Modify the warning message about scandirat(3)
It should mention scandirat() instead of scandir().
--- tools/perf/util/print-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/print-events.c +1 -1
@@ -131,7 +131,7 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus free(sys_namelist); }#else -printf ("\nWARNING: Your libc doesn't have the scandir function, please ask its maintainers to implement it.\n" +printf ("\nWARNING: Your libc doesn't have the scandirat function, please ask its maintainers to implement it.\n" " As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n" , events_path);#endif close (events_fd);
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
Re: [PATCH 2/2] perf list: Modify the warning message about scandirat(3)
Quoted 4 lines · Namhyung Kim
It should mention
scandirat()instead ofscandir().Signed-off-by Namhyung Kim <[email protected]>
Fwiw, tracing_events__scandir_alphasort assumes scandir is present unconditionally.
Thanks,
Ian
Quoted 20 lines
--- tools/perf/util/print-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/print-events.c +1
-2@@ -131,7 +131,7 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus free(sys_namelist); }#else -printf ("\nWARNING: Your libc doesn't have the scandir function, please ask its maintainers to implement it.\n" +printf ("\nWARNING: Your libc doesn't have the scandirat function, please ask its maintainers to implement it.\n" " As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n" , events_path);#endif close (events_fd); --2.40.1.495.gc816e09b53d-goog
Quoted 5 lines · Namhyung Kim
It should free entries (not only the array) filled by
scandirat()after use.Signed-off-by Namhyung Kim <[email protected]>
Thanks,
Ian
Quoted 54 lines
--- tools/perf/util/print-events.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)tools/perf/util/print-events.c +8
-5@@ -83,11 +83,11 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unusif (sys_dirent->d_type !=DT_DIR || !strcmp (sys_dirent->d_name,"." ) || !strcmp (sys_dirent->d_name,".." )) -continue ; + goto next_sys; dir_fd =openat (events_fd, sys_dirent->d_name,O_PATH );if (dir_fd <0 ) -continue ; + goto next_sys; evt_items =scandirat (events_fd, sys_dirent->d_name, &evt_namelist,NULL , alphasort);for (int j =0 ; j < evt_items; j++) {@@ -98,12 +98,12 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unusif (evt_dirent->d_type !=DT_DIR || !strcmp (evt_dirent->d_name,"." ) || !strcmp (evt_dirent->d_name,".." )) -continue ; + goto next_evt;snprintf (evt_path,sizeof (evt_path),"%s/id" , evt_dirent->d_name); evt_fd =openat (dir_fd, evt_path,O_RDONLY );if (evt_fd <0 ) -continue ; + goto next_evt;close (evt_fd); snprintf(evt_path, MAXPATHLEN, "%s:%s",@@ -119,9 +119,13 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus/*desc=*/ NULL ,/*long_desc=*/ NULL , /*encoding_desc=*/NULL); +next_evt: +free (evt_namelist[j]); }close (dir_fd);free (evt_namelist); +next_sys: +free (sys_namelist[i]); }free (sys_namelist); --2.40.1.495.gc816e09b53d-goog
Em Thu, Apr 27, 2023 at 05:27:50PM -0700, Ian Rogers escreveu:
Quoted 8 lines · Namhyung Kim
Quoted 5 lines
It should free entries (not only the array) filled by
scandirat()after use.Signed-off-by Namhyung Kim <[email protected]>Reviewed-by Ian Rogers <[email protected]>
Thanks, applied the series.
- Arnaldo
Quoted 57 lines
Thanks,
IanQuoted 54 lines
--- tools/perf/util/print-events.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)tools/perf/util/print-events.c +8
-5@@ -83,11 +83,11 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unusif (sys_dirent->d_type !=DT_DIR || !strcmp (sys_dirent->d_name,"." ) || !strcmp (sys_dirent->d_name,".." )) -continue ; + goto next_sys; dir_fd =openat (events_fd, sys_dirent->d_name,O_PATH );if (dir_fd <0 ) -continue ; + goto next_sys; evt_items =scandirat (events_fd, sys_dirent->d_name, &evt_namelist,NULL , alphasort);for (int j =0 ; j < evt_items; j++) {@@ -98,12 +98,12 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unusif (evt_dirent->d_type !=DT_DIR || !strcmp (evt_dirent->d_name,"." ) || !strcmp (evt_dirent->d_name,".." )) -continue ; + goto next_evt;snprintf (evt_path,sizeof (evt_path),"%s/id" , evt_dirent->d_name); evt_fd =openat (dir_fd, evt_path,O_RDONLY );if (evt_fd <0 ) -continue ; + goto next_evt;close (evt_fd); snprintf(evt_path, MAXPATHLEN, "%s:%s",@@ -119,9 +119,13 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus/*desc=*/ NULL ,/*long_desc=*/ NULL , /*encoding_desc=*/NULL); +next_evt: +free (evt_namelist[j]); }close (dir_fd);free (evt_namelist); +next_sys: +free (sys_namelist[i]); }free (sys_namelist); --2.40.1.495.gc816e09b53d-goog
Signature · 1 line
- Arnaldo