From a30faae0930d348838d09d3f135f04c91a05d8d0 Mon Sep 17 00:00:00 2001 From: Rongqi Sun Date: Wed, 8 May 2024 08:16:13 +0000 Subject: [PATCH] crush/builder: free 'crush_rule' before return When sanitizer is enabled, unittest_erasure_code_shec shows: ``` ================================================================= ==437976==ERROR: LeakSanitizer: detected memory leaks Direct leak of 2021708 byte(s) in 29731 object(s) allocated from: #0 0xaaaab0144820 in malloc (/root/ceph-19.0.0/build/bin/unittest_erasure_code_shec+0x1d4820) (BuildId: c0999ecf82504ed7e184ea4b4b9ae9d32faa1c46) #1 0xffffa770057c in crush_make_rule /root/ceph-19.0.0/src/crush/builder.c:104:9 #2 0xffffa7751638 in CrushWrapper::add_simple_rule_at(std::__cxx11::basic_string, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::__cxx11::basic_string, std::allocator >, int, std::__cxx11::basic_string, std::allocator >, std::__cxx11::basic_string, std::allocator >, int, int, std::ostream*) /root/ceph-19.0.0/src/crush/CrushWrapper.cc:2327:22 #3 0xffffa7751d2c in CrushWrapper::add_simple_rule(std::__cxx11::basic_string, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::__cxx11::basic_string, std::allocator >, int, std::__cxx11::basic_string, std::allocator >, std::__cxx11::basic_string, std::allocator >, int, std::ostream*) /root/ceph-19.0.0/src/crush/CrushWrapper.cc:2369:10 #4 0xffffa39d6198 in ceph::ErasureCode::create_rule(std::__cxx11::basic_string, std::allocator > const&, CrushWrapper&, std::ostream*) const /root/ceph-19.0.0/src/erasure-code/ErasureCode.cc:76:18 #5 0xaaaab01f5a6c in thread3(void*) /root/ceph-19.0.0/src/test/erasure-code/TestErasureCodeShec.cc:2756:11 #6 0xffffa2dad5c4 in start_thread nptl/./nptl/pthread_create.c:442:8 #7 0xffffa2e15ed8 misc/../sysdeps/unix/sysv/linux/aarch64/clone.S:79 SUMMARY: AddressSanitizer: 2021708 byte(s) leaked in 29731 allocation(s). ``` When crush_add_rule exceeds the number of max_rules, crush_rule shoule be freed right before returning. Signed-off-by: Rongqi Sun --- src/crush/builder.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/crush/builder.c b/src/crush/builder.c index 383f3e710de6b6..7061805143d6e4 100644 --- a/src/crush/builder.c +++ b/src/crush/builder.c @@ -82,6 +82,7 @@ int crush_add_rule(struct crush_map *map, struct crush_rule *rule, int ruleno) int oldsize; void *_realloc = NULL; if (map->max_rules +1 > CRUSH_MAX_RULES) + free(rule); return -ENOSPC; oldsize = map->max_rules; map->max_rules = r+1;