selftests/cgroup: free the paths returned by cg_control()
Subsystems (4)
PATCH 1/1 selftests/cgroup: free the paths returned by cg_control()
cg_control() returns a path allocated with malloc(). The two callers that pass it straight to inotify_add_watch() never free it, and pass NULL to it if the allocation fails.
Keep the path in a local variable, check it, and free it when done.
--- tools/testing/selftests/cgroup/lib/cgroup_util.c | 13 ++++++++++--- tools/testing/selftests/cgroup/test_memcontrol.c | 9 +++++++-- 2 files changed, 17 insertions(+), 5 deletions(-)
tools/testing/selftests/cgroup/lib/cgroup_util.c +10 -3
@@ -626,18 +626,25 @@ int clone_into_cgroup_run_wait(const char *cgroup)static int __prepare_for_wait (const char *cgroup,const char *filename) { -int fd, ret =-1 ; +char *path; +int fd, ret; + + path =cg_control (cgroup, filename); +if (!path) +return -1 ; fd =inotify_init1 (0 );if (fd ==-1 ) -return fd; + goto out; - ret =inotify_add_watch (fd,cg_control (cgroup, filename),IN_MODIFY ); + ret =inotify_add_watch (fd, path,IN_MODIFY );if (ret ==-1 ) {close (fd); fd =-1 ; } +out: +free (path);return fd; }
tools/testing/selftests/cgroup/test_memcontrol.c +7 -2
@@ -1677,7 +1677,7 @@ static int read_event(int inotify_fd, int expected_event, int expected_wd)static int test_memcg_inotify_delete_file (const char *root) {int ret =KSFT_FAIL ; -char *memcg =NULL ; +char *memcg =NULL , *events =NULL ;int fd =-1 , wd; memcg =cg_name (root,"memcg_test_0" );
@@ -1692,7 +1692,11 @@ static int test_memcg_inotify_delete_file(const char *root)if (fd ==-1 ) goto cleanup; - wd =inotify_add_watch (fd,cg_control (memcg,"memory.events" ),IN_DELETE_SELF ); + events =cg_control (memcg,"memory.events" ); +if (!events) + goto cleanup; + + wd =inotify_add_watch (fd, events,IN_DELETE_SELF );if (wd ==-1 ) goto cleanup;
@@ -1714,6 +1718,7 @@ static int test_memcg_inotify_delete_file(const char *root)close (fd);if (memcg)cg_destroy (memcg); +free (events);free (memcg);return ret;
Signature · 1 line
2.53.0
Quoted 6 lines · Sasha Levin
cg_control()returns a path allocated withmalloc(). The two callers that pass it straight toinotify_add_watch()never free it, and pass NULL to it if the allocation fails.Keep the path in a local variable, check it, and free it when done.
Quoted 8 lines
Fixes 5313bfe425c8a ("kselftests: cgroup: add freezer controller self-tests")Fixes 2de27980e1d46 ("selftests: memcg: Add tests for IN_DELETE_SELF and IN_IGNORED")Assisted-by LLMSigned-off-by Sasha Levin <[email protected]>--- tools/testing/selftests/cgroup/lib/cgroup_util.c | 13 ++++++++++--- tools/testing/selftests/cgroup/test_memcontrol.c | 9 +++++++-- 2 files changed, 17 insertions(+), 5 deletions(-)
Thanks,
Quoted 11 lines · Sasha Levin
cg_control()returns a path allocated withmalloc(). The two callers that pass it straight toinotify_add_watch()never free it, and pass NULL to it if the allocation fails.Keep the path in a local variable, check it, and free it when done.
Fixes 5313bfe425c8a ("kselftests: cgroup: add freezer controller self-tests")Fixes 2de27980e1d46 ("selftests: memcg: Add tests for IN_DELETE_SELF and IN_IGNORED")Assisted-by LLMSigned-off-by Sasha Levin <[email protected]>
Quoted 10 lines · Sasha Levin
cg_control()returns a path allocated withmalloc(). The two callers that pass it straight toinotify_add_watch()never free it, and pass NULL to it if the allocation fails.Keep the path in a local variable, check it, and free it when done.
Fixes 5313bfe425c8a ("kselftests: cgroup: add freezer controller self-tests")Fixes 2de27980e1d46 ("selftests: memcg: Add tests for IN_DELETE_SELF and IN_IGNORED")Assisted-by LLMSigned-off-by Sasha Levin <[email protected]>
Applied to cgroup/for-7.4 with the subject capitalized and the Fixes tags dropped.
Thanks.
Signature · 1 line
tejun