]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/mon/test_config_map: free CrushWrapper created with new 56461/head
authorKefu Chai <tchaikov@gmail.com>
Tue, 26 Mar 2024 03:03:38 +0000 (11:03 +0800)
committerKefu Chai <tchaikov@gmail.com>
Tue, 26 Mar 2024 03:06:54 +0000 (11:06 +0800)
before this change, we create a new CrushWrapper instance with `new`, but
we never free this instance after done with it. and LeakSanitizer
points this out:

```
Direct leak of 544 byte(s) in 1 object(s) allocated from:
    #0 0x561afe148fed in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_config_map+0x1c2fed) (BuildId: 3ce9eeed38cee335628fa74fdd08cd215b15019e)
    #1 0x561afe151cbd in ConfigMap_result_sections_Test::TestBody() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/mon/test_config_map.cc:93:16
    #2 0x561afe2689b6 in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10
    #3 0x561afe221262 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14
    #4 0x561afe1d1f7c in testing::Test::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2680:5
    #5 0x561afe1d3fb2 in testing::TestInfo::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2858:11
    #6 0x561afe1d55eb in testing::TestSuite::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:3012:28
    #7 0x561afe1f2a78 in testing::internal::UnitTestImpl::RunAllTests() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5723:44
    #8 0x561afe2711e6 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10
    #9 0x561afe227bd2 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14
    #10 0x561afe1f1e02 in testing::UnitTest::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5306:10
    #11 0x561afe176ec0 in RUN_ALL_TESTS() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/include/gtest/gtest.h:2486:46
    #12 0x561afe176e51 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googlemock/src/gmock_main.cc:70:10
    #13 0x7f37d9397d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
```

so in this change, we manage the `CrushWrapper` pointer with a smart
pointer. because the size of `CrushWrapper` is relatively large, we
don't create it on stack.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/test/mon/test_config_map.cc

index 75fbe1ce526e2dca614b52450bc07ef923f57ad7..e147db9779afd1627235341808f97009c452bb23 100644 (file)
@@ -4,6 +4,7 @@
 #include "mon/ConfigMap.h"
 
 #include <iostream>
+#include <memory>
 #include <string>
 #include "crush/CrushWrapper.h"
 #include "common/ceph_context.h"
@@ -90,7 +91,7 @@ TEST(ConfigMap, result_sections)
 {
   ConfigMap cm;
   boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
-  auto crush = new CrushWrapper;
+  auto crush = std::make_unique<CrushWrapper>();
   crush->finalize();
 
   int r;
@@ -124,19 +125,19 @@ TEST(ConfigMap, result_sections)
   EntityName n;
   n.set(CEPH_ENTITY_TYPE_MON, "a");
   auto c = cm.generate_entity_map(
-    n, {}, crush, "none", nullptr);
+    n, {}, crush.get(), "none", nullptr);
   ASSERT_EQ(1, c.size());
   ASSERT_EQ("a", c["foo"]);
 
   n.set(CEPH_ENTITY_TYPE_MON, "b");
   c = cm.generate_entity_map(
-    n, {}, crush, "none", nullptr);
+    n, {}, crush.get(), "none", nullptr);
   ASSERT_EQ(1, c.size());
   ASSERT_EQ("m", c["foo"]);
 
   n.set(CEPH_ENTITY_TYPE_MDS, "c");
   c = cm.generate_entity_map(
-    n, {}, crush, "none", nullptr);
+    n, {}, crush.get(), "none", nullptr);
   ASSERT_EQ(1, c.size());
   ASSERT_EQ("g", c["foo"]);
 }