From 8c5583d7a54ac98dea980eea1b4a0a6b0e2f1a07 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Fri, 9 Sep 2022 14:48:32 +0200 Subject: [PATCH] Fix stack-use-after-return in GetPriorityForLoadBalancing::getPriorityFunc() clang-15 reports [1]:
ASan report ``` ==1==ERROR: AddressSanitizer: stack-use-after-return on address 0x7f1d04c4eb20 at pc 0x000031c4803c bp 0x7f1d05e19a00 sp 0x7f1d05e199f8 READ of size 8 at 0x7f1d04c4eb20 thread T200 (QueryPullPipeEx) #0 0x31c4803b in DB::GetPriorityForLoadBalancing::getPriorityFunc(DB::LoadBalancing, unsigned long, unsigned long) const::$_3::operator()(unsigned long) const build_docker/../src/Common/GetPriorityForLoadBalancing.cpp:42:40 #1 0x31c4803b in decltype(static_cast(fp)(static_cast(fp0))) std::__1::__invoke(DB::GetPriorityForLoadBalancing::getPriorityFunc(DB::LoadBalancing, unsigned long, unsigned long) const::$_3&, unsigned long&&) build_docker/../contrib/libcxx/include/type_traits:3640:23 #2 0x31c4803b in unsigned long std::__1::__invoke_void_return_wrapper::__call(DB::GetPriorityForLoadBalancing::getPriorityFunc(DB::LoadBalancing, unsigned long, unsigned long) const::$_3&, unsigned long&&) build_docker/../contrib/libcxx/include/__functional/invoke.h:30:16 #3 0x31c4803b in std::__1::__function::__default_alloc_func::operator()(unsigned long&&) build_docker/../contrib/libcxx/include/__functional/function.h:230:12 #4 0x31c4803b in unsigned long std::__1::__function::__policy_invoker::__call_impl>(std::__1::__function::__policy_storage const*, unsigned long) build_docker/../contrib/libcxx/include/__functional/function.h:711:16 #5 0x31c38b07 in std::__1::__function::__policy_func::operator()(unsigned long&&) const build_docker/../contrib/libcxx/include/__functional/function.h:843:16 #6 0x31c38b07 in std::__1::function::operator()(unsigned long) const build_docker/../contrib/libcxx/include/__functional/function.h:1184:12 #7 0x31c38b07 in PoolWithFailoverBase::getShuffledPools(unsigned long, std::__1::function const&) build_docker/../src/Common/PoolWithFailoverBase.h:174:39 This frame has 2 object(s): [32, 40) 'pool_size.addr' <== Memory access at offset 32 is inside this variable [64, 88) 'ref.tmp' (line 18) ```
[1]: https://s3.amazonaws.com/clickhouse-test-reports/41046/adea92f847373d1fcfd733d8979c63024f9b80bf/integration_tests__asan__[1/3].html Signed-off-by: Azat Khuzhin --- src/Common/GetPriorityForLoadBalancing.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/GetPriorityForLoadBalancing.cpp b/src/Common/GetPriorityForLoadBalancing.cpp index d8e7566e891..5da60fb1bae 100644 --- a/src/Common/GetPriorityForLoadBalancing.cpp +++ b/src/Common/GetPriorityForLoadBalancing.cpp @@ -16,7 +16,7 @@ std::function GetPriorityForLoadBalancing::getPriorityFunc case LoadBalancing::NEAREST_HOSTNAME: if (hostname_differences.empty()) throw Exception(ErrorCodes::LOGICAL_ERROR, "It's a bug: hostname_differences is not initialized"); - get_priority = [&](size_t i) { return hostname_differences[i]; }; + get_priority = [this](size_t i) { return hostname_differences[i]; }; break; case LoadBalancing::IN_ORDER: get_priority = [](size_t i) { return i; }; @@ -36,7 +36,7 @@ std::function GetPriorityForLoadBalancing::getPriorityFunc * last_used = 3 -> get_priority: 4 3 0 1 2 * ... * */ - get_priority = [&](size_t i) + get_priority = [this, pool_size](size_t i) { ++i; return i < last_used ? pool_size - i : i - last_used;