]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
gunit/017: release fuzz cgroups during test cleanup
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Thu, 26 Feb 2026 06:39:37 +0000 (12:09 +0530)
committerKamalesh Babulal <kamalesh.babulal@oracle.com>
Tue, 10 Mar 2026 16:38:04 +0000 (22:08 +0530)
ASan reported that APIArgsTest_API_cgroup_get_uid_gid_Test leaked the
struct cgroup returned by cgroup_new_cgroup():

==2840589==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4928 byte(s) in 1 object(s) allocated from:
.../tests/gunit/017-API_fuzz_test.cpp:287

APIArgsTest constructs several valid cgroups but doesn’t release them,
so each sanitizer run leaves their heap allocations behind. The test
now tracks the cgroups it creates via a NewTestCgroup() helper and
releases them from TearDown(), and all positive cgroup_new_cgroup()
call sites in the file use that helper. This mirrors the expected
cgroup_free() cleanup, keeps the fuzz-style tests self-contained, and
silences the ASan leak report.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
tests/gunit/017-API_fuzz_test.cpp

index 049de34f0ac9c0840441888fe69e37d7d109ad4b..a586c321584b42c54f5547b3512be27ae834ce4e 100644 (file)
@@ -7,17 +7,40 @@
  */
 
 #include <sys/stat.h>
+#include <stdlib.h>
+#include <vector>
 
 #include "gtest/gtest.h"
 
 #include "libcgroup-internal.h"
 
-class APIArgsTest: public :: testing:: Test {
-       protected:
+class APIArgsTest : public ::testing::Test {
+protected:
+       std::vector<struct cgroup *> test_cgroups_;
 
-       void SetUp() override {
+       struct cgroup *NewTestCgroup(const char *name)
+       {
+               struct cgroup *cgrp = cgroup_new_cgroup(name);
+
+               if (cgrp)
+                       test_cgroups_.push_back(cgrp);
+
+               return cgrp;
+       }
+
+       void SetUp() override
+       {
                /* Stub */
        }
+
+       void TearDown() override
+       {
+               for (struct cgroup *&cgrp : test_cgroups_) {
+                       if (cgrp)
+                               cgroup_free(&cgrp);
+               }
+               test_cgroups_.clear();
+       }
 };
 
 /**
@@ -85,7 +108,7 @@ TEST_F(APIArgsTest, API_cgroup_set_value_string)
        ret = cgroup_set_value_string(cgc, name, value);
        ASSERT_EQ(ret, 50011);
 
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -141,7 +164,7 @@ TEST_F(APIArgsTest, API_cgroup_get_value_string)
        ret = cgroup_get_value_string(cgc, name, NULL);
        ASSERT_EQ(ret, 50011);
 
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -198,7 +221,7 @@ TEST_F(APIArgsTest, API_cgroup_add_controller)
        cgc = cgroup_add_controller(cgroup, cgrp_ctrl);
        ASSERT_EQ(cgc, nullptr);
 
-       cgroup = cgroup_new_cgroup(cgrp_name);
+       cgroup = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgroup, nullptr);
 
        cgc = cgroup_add_controller(cgroup, cgrp_ctrl);
@@ -236,7 +259,7 @@ TEST_F(APIArgsTest, API_cgroup_add_value_string)
 
        // case 2
        // cgc = valid, name = NULL, value = NULL
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -284,7 +307,7 @@ TEST_F(APIArgsTest, API_cgroup_get_uid_gid)
        // case 2
        // cgroup = valid, tasks_uid = NULL, tasks_gid = NULL, control_uid = NULL,
        // control_uid = NULL
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        ret = cgroup_get_uid_gid(cgrp, NULL, NULL, NULL, NULL);
@@ -341,7 +364,7 @@ TEST_F(APIArgsTest, API_cgroup_set_value_int64)
 
        // case 2
        // cgc = valid, name = NULL, value = valid
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -396,7 +419,7 @@ TEST_F(APIArgsTest, API_cgroup_get_value_int64)
 
        // case 2
        // cgc = valid, name = NULL, value = NULL
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -452,7 +475,7 @@ TEST_F(APIArgsTest, API_cgroup_set_value_uint64)
 
        // case 2
        // cgc = valid, name = NULL, value = valid
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -507,7 +530,7 @@ TEST_F(APIArgsTest, API_cgroup_get_value_uint64)
 
        // case 2
        // cgc = valid, name = NULL, value = NULL
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -563,7 +586,7 @@ TEST_F(APIArgsTest, API_cgroup_set_value_bool)
 
        // case 2
        // cgc = valid, name = NULL, value = valid
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);
@@ -618,7 +641,7 @@ TEST_F(APIArgsTest, API_cgroup_get_value_bool)
 
        // case 2
        // cgc = valid, name = NULL, value = NULL
-       cgrp = cgroup_new_cgroup(cgrp_name);
+       cgrp = NewTestCgroup(cgrp_name);
        ASSERT_NE(cgrp, nullptr);
 
        cgc = cgroup_add_controller(cgrp, cgrp_ctrl);