This is an automated email from the ASF dual-hosted git repository. silver pushed a commit to branch cpp-contribute-docs in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit 6728d0f19c63fbeb7ea1f874deb2b3ee78544b25 Author: silver-ymz <[email protected]> AuthorDate: Thu Aug 31 13:51:17 2023 +0800 add address sanitizer to check memory error Signed-off-by: silver-ymz <[email protected]> --- bindings/cpp/CMakeLists.txt | 6 ++++++ bindings/cpp/src/opendal.cpp | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bindings/cpp/CMakeLists.txt b/bindings/cpp/CMakeLists.txt index b8b5abc1d..226b99749 100644 --- a/bindings/cpp/CMakeLists.txt +++ b/bindings/cpp/CMakeLists.txt @@ -65,6 +65,12 @@ target_include_directories(opendal_cpp_test PUBLIC ${CPP_INCLUDE_DIR} ${GTEST_IN target_link_libraries(opendal_cpp_test ${GTEST_LDFLAGS} GTest::gtest_main opendal_cpp) target_compile_options(opendal_cpp_test PRIVATE ${GTEST_CFLAGS}) +# enable address sanitizers +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") + target_compile_options(opendal_cpp_test PRIVATE -fsanitize=leak,address,undefined -fno-omit-frame-pointer -fno-common -O1) + target_link_options(opendal_cpp_test PRIVATE -fsanitize=leak,address,undefined) +endif() + # Platform-specific test configuration if(WIN32) target_link_libraries(opendal_cpp_test userenv ws2_32 bcrypt) diff --git a/bindings/cpp/src/opendal.cpp b/bindings/cpp/src/opendal.cpp index 92c240c3e..49a1fa227 100644 --- a/bindings/cpp/src/opendal.cpp +++ b/bindings/cpp/src/opendal.cpp @@ -38,8 +38,16 @@ Operator::Operator(std::string_view scheme, bool Operator::available() const { return operator_.has_value(); } std::vector<uint8_t> Operator::read(std::string_view path) { - auto res = operator_.value()->read(rust::Str(path.data())); - return std::vector<uint8_t>(res.data(), res.data() + res.size()); + auto rust_vec = operator_.value()->read(rust::Str(path.data())); + + // Convert rust::Vec<uint8_t> to std::vector<uint8_t> + // This cannot use rust vector pointer to init std::vector because + // rust::Vec owns the memory and will free it when it goes out of scope. + std::vector<uint8_t> res; + res.reserve(rust_vec.size()); + std::copy(rust_vec.cbegin(), rust_vec.cend(), std::back_inserter(res)); + + return res; } void Operator::write(std::string_view path, const std::vector<uint8_t> &data) {
