From: xie xingguo Date: Tue, 18 Jul 2017 10:49:45 +0000 (+0800) Subject: test/crush: fix memory leak X-Git-Tag: v12.1.2~68^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=1ed72c3e861283f83580449f53a627c317d1b2a5;p=ceph.git test/crush: fix memory leak use unique_ptr<> instead, otherwise static analyzer will complain. as ASSERT_EQ will bail out without executing delete c Signed-off-by: xie xingguo --- diff --git a/src/test/crush/CrushWrapper.cc b/src/test/crush/CrushWrapper.cc index 6dd95220fe7b..fd85d55bd8a6 100644 --- a/src/test/crush/CrushWrapper.cc +++ b/src/test/crush/CrushWrapper.cc @@ -33,8 +33,8 @@ #include "crush/CrushWrapper.h" TEST(CrushWrapper, get_immediate_parent) { - CrushWrapper *c = new CrushWrapper; - + std::unique_ptr c(new CrushWrapper); + const int ROOT_TYPE = 1; c->set_type_name(ROOT_TYPE, "root"); const int OSD_TYPE = 0; @@ -64,12 +64,10 @@ TEST(CrushWrapper, get_immediate_parent) { EXPECT_EQ(0, ret); EXPECT_EQ("root", loc.first); EXPECT_EQ("default", loc.second); - - delete c; } TEST(CrushWrapper, move_bucket) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -124,12 +122,10 @@ TEST(CrushWrapper, move_bucket) { EXPECT_EQ("root", loc.first); EXPECT_EQ("root1", loc.second); } - - delete c; } TEST(CrushWrapper, swap_bucket) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -192,7 +188,7 @@ TEST(CrushWrapper, swap_bucket) { } TEST(CrushWrapper, rename_bucket_or_item) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -256,12 +252,10 @@ TEST(CrushWrapper, rename_bucket_or_item) { int osd0id = c->get_item_id("osd0"); EXPECT_EQ(0, c->rename_item("osd.0", "osd0renamed", &ss)); EXPECT_EQ(osd0id, c->get_item_id("osd0renamed")); - - delete c; } TEST(CrushWrapper, check_item_loc) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); int item = 0; float expected_weight = 1.0; @@ -323,12 +317,10 @@ TEST(CrushWrapper, check_item_loc) { EXPECT_TRUE(c->check_item_loc(g_ceph_context, item, loc, &weight)); EXPECT_EQ(expected_weight, weight); } - - delete c; } TEST(CrushWrapper, update_item) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -419,12 +411,10 @@ TEST(CrushWrapper, update_item) { EXPECT_EQ(modified_weight, c->get_item_weightf(item)); EXPECT_FALSE(c->check_item_loc(g_ceph_context, item, loc, &weight)); EXPECT_TRUE(c->check_item_loc(g_ceph_context, item, other_loc, &weight)); - - delete c; } TEST(CrushWrapper, adjust_item_weight) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -540,12 +530,10 @@ TEST(CrushWrapper, adjust_item_weight) { EXPECT_EQ(1, c->adjust_item_weightf_in_loc(g_ceph_context, item, modified_weight, loc_two)); EXPECT_EQ(original_weight, c->get_item_weightf_in_loc(item, loc_one)); EXPECT_EQ(modified_weight, c->get_item_weightf_in_loc(item, loc_two)); - - delete c; } TEST(CrushWrapper, adjust_subtree_weight) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -635,12 +623,10 @@ TEST(CrushWrapper, adjust_subtree_weight) { ASSERT_EQ(c->get_bucket_weight(host0), 262144); ASSERT_EQ(c->get_item_weight(host0), 262144); ASSERT_EQ(c->get_bucket_weight(rootno), 262144 + 131072); - - delete c; } TEST(CrushWrapper, insert_item) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -771,12 +757,10 @@ TEST(CrushWrapper, insert_item) { EXPECT_EQ(-EINVAL, c->insert_item(g_ceph_context, item, 1.0, "osd." + stringify(item), loc)); } - - delete c; } TEST(CrushWrapper, remove_item) { - auto *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 2; c->set_type_name(ROOT_TYPE, "root"); @@ -815,12 +799,10 @@ TEST(CrushWrapper, remove_item) { ASSERT_EQ(0, c->remove_item(g_ceph_context, item_to_remove, true)); float weight; EXPECT_FALSE(c->check_item_loc(g_ceph_context, item_to_remove, loc, &weight)); - - delete c; } TEST(CrushWrapper, item_bucket_names) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); int index = 123; string name = "NAME"; EXPECT_EQ(-EINVAL, c->set_item_name(index, "\001")); @@ -829,18 +811,16 @@ TEST(CrushWrapper, item_bucket_names) { EXPECT_TRUE(c->item_exists(index)); EXPECT_EQ(index, c->get_item_id(name)); EXPECT_EQ(name, c->get_item_name(index)); - delete c; } TEST(CrushWrapper, bucket_types) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); int index = 123; string name = "NAME"; c->set_type_name(index, name); EXPECT_EQ(1, c->get_num_type_names()); EXPECT_EQ(index, c->get_type_id(name)); EXPECT_EQ(name, c->get_type_name(index)); - delete c; } TEST(CrushWrapper, is_valid_crush_name) { @@ -867,7 +847,7 @@ TEST(CrushWrapper, is_valid_crush_loc) { } TEST(CrushWrapper, dump_rules) { - CrushWrapper *c = new CrushWrapper; + std::unique_ptr c(new CrushWrapper); const int ROOT_TYPE = 1; c->set_type_name(ROOT_TYPE, "root"); @@ -937,8 +917,6 @@ TEST(CrushWrapper, dump_rules) { c->get_rule_weight_osd_map(0, &wm); ASSERT_TRUE(wm.size() == 1); ASSERT_TRUE(wm[0] == 1.0); - - delete c; } TEST(CrushWrapper, distance) {