From: Oguzhan Ozmen Date: Sat, 8 Nov 2025 19:28:19 +0000 (+0000) Subject: RGW: make SSTR macro safe against variable name collisions X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=a018ca5b8f67f51b544e34d2cf5fa800a447df68;p=ceph-ci.git RGW: make SSTR macro safe against variable name collisions The original SSTR macro defined a local variable `std::stringstream ss` inside its expansion. When an expression that used a variable named `ss` is passed to this macro, it led to name shadowing and unexpected results. Example: std::stringstream ss; ss << "aaa"; auto result = SSTR("this is ss=" << ss.str()); Actual result: "this is ss=this is ss=" Expected result: "this is ss=aaa" This change rewrites the macro to construct an unnamed temporary to avoid any name collision. Signed-off-by: Oguzhan Ozmen --- diff --git a/src/rgw/driver/rados/rgw_sync_trace.h b/src/rgw/driver/rados/rgw_sync_trace.h index 9686d19271e..074844ad501 100644 --- a/src/rgw/driver/rados/rgw_sync_trace.h +++ b/src/rgw/driver/rados/rgw_sync_trace.h @@ -15,11 +15,7 @@ #include #include -#define SSTR(o) ({ \ - std::stringstream ss; \ - ss << o; \ - ss.str(); \ -}) +#define SSTR(o) ((std::ostringstream{} << o).str()) #define RGW_SNS_FLAG_ACTIVE 1 #define RGW_SNS_FLAG_ERROR 2 diff --git a/src/test/rgw/CMakeLists.txt b/src/test/rgw/CMakeLists.txt index eabbc73fc0a..4fd8d390483 100644 --- a/src/test/rgw/CMakeLists.txt +++ b/src/test/rgw/CMakeLists.txt @@ -316,6 +316,13 @@ target_include_directories(unittest_rgw_url SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/src/rgw") target_link_libraries(unittest_rgw_url ${rgw_libs}) +# unittest_rgw_sync_trace +add_executable(unittest_rgw_sync_trace test_rgw_sync_trace.cc) +add_ceph_unittest(unittest_rgw_sync_trace) +target_include_directories(unittest_rgw_sync_trace + SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/src/rgw") +target_link_libraries(unittest_rgw_sync_trace) + if(WITH_RADOSGW_RADOS) add_executable(ceph_test_rgw_gc_log test_rgw_gc_log.cc $) target_include_directories(ceph_test_rgw_gc_log diff --git a/src/test/rgw/test_rgw_sync_trace.cc b/src/test/rgw/test_rgw_sync_trace.cc new file mode 100644 index 00000000000..d0bcca06613 --- /dev/null +++ b/src/test/rgw/test_rgw_sync_trace.cc @@ -0,0 +1,23 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- +// vim: ts=8 sw=2 sts=2 expandtab ft=cpp + +#include +#include "rgw/driver/rados/rgw_sync_trace.h" + +TEST(TestSSTR, using_a_var_named_ss) +{ + std::stringstream ss; + + ss << "aaa"; + auto result = SSTR("this is ss=" << ss.str()); + ASSERT_EQ(result, "this is ss=aaa"); +} + +TEST(TestSSTR, using_a_var_named_other_than_ss) +{ + std::stringstream ss2; + + ss2 << "aaa"; + auto result = SSTR("this is ss2=" << ss2.str()); + ASSERT_EQ(result, "this is ss2=aaa"); +}