acelyc111 commented on a change in pull request #3495:
URL: https://github.com/apache/incubator-doris/pull/3495#discussion_r421923066
##########
File path: be/src/util/path_util.cpp
##########
@@ -70,13 +70,19 @@ vector<string> split_path(const string& path) {
return segments;
}
+struct FreeDeleter {
+ inline void operator()(void* ptr) const {
+ free(ptr);
+ }
+};
+
string dir_name(const string& path) {
- std::unique_ptr<char[]> path_copy(strdup(path.c_str()));
+ std::unique_ptr<char[], FreeDeleter> path_copy(strdup(path.c_str()));
Review comment:
> Would better change to
>
> ```
> string dir_name(const string& path) {
> std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() +
1);
> return dirname(&path_copy[0]);
> }
>
> string base_name(const string& path) {
> std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() +
1);
> return basename(&path_copy[0]);
> }
> ```
ASAN will report an error if using `std::vector<char>`, seems '\0' is not
found when pass `&path_copy[0]` to a `char*` parameter.
```
$ ./be/build/test/util/path_util_test
[==========] Running 5 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 5 tests from TestPathUtil
[ RUN ] TestPathUtil.JoinPathSegments
[ OK ] TestPathUtil.JoinPathSegments (0 ms)
[ RUN ] TestPathUtil.BaseNameTest
=================================================================
==133070==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x602000000b91 at pc 0x000000502ca4 bp 0x7ffc412f28b0 sp 0x7ffc412f2060
READ of size 2 at 0x602000000b91 thread T0
#0 0x502ca3 in __interceptor_strlen
../../.././libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:225
#1 0x5f4edf in std::char_traits<char>::length(char const*)
/usr/include/c++/7.3.0/bits/char_traits.h:320
#2 0x5f4edf in std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
/usr/include/c++/7.3.0/bits/basic_string.h:511
#3 0x5f4edf in
doris::path_util::base_name(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)
/home/laiyingchun/ap_doris/be/src/util/path_util.cpp:80
#4 0x5da0fa in doris::TestPathUtil_BaseNameTest_Test::TestBody()
/home/laiyingchun/ap_doris/be/test/util/path_util_test.cpp:44
#5 0x6c6d54 in void
testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test,
void>(testing::Test*, void (testing::Test::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c6d54)
#6 0x6c15ad in void
testing::internal::HandleExceptionsInMethodIfSupported<testing::Test,
void>(testing::Test*, void (testing::Test::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c15ad)
#7 0x6a686f in testing::Test::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6a686f)
#8 0x6a70f1 in testing::TestInfo::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6a70f1)
#9 0x6a7748 in testing::TestCase::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6a7748)
#10 0x6ae2bc in testing::internal::UnitTestImpl::RunAllTests()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6ae2bc)
#11 0x6c7d6a in bool
testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl,
bool>(testing::internal::UnitTestImpl*, bool
(testing::internal::UnitTestImpl::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c7d6a)
#12 0x6c22f9 in bool
testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl,
bool>(testing::internal::UnitTestImpl*, bool
(testing::internal::UnitTestImpl::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c22f9)
#13 0x6acf93 in testing::UnitTest::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6acf93)
#14 0x5c9801 in RUN_ALL_TESTS()
/home/laiyingchun/ap_doris/thirdparty/installed/include/gtest/gtest.h:2233
#15 0x5c9801 in main
/home/laiyingchun/ap_doris/be/test/util/path_util_test.cpp:113
#16 0x7fec8e60f504 in __libc_start_main (/lib64/libc.so.6+0x22504)
#17 0x4d3f64
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x4d3f64)
0x602000000b91 is located 0 bytes to the right of 1-byte region
[0x602000000b90,0x602000000b91)
allocated by thread T0 here:
#0 0x58e620 in operator new(unsigned long)
../../.././libsanitizer/asan/asan_new_delete.cc:80
#1 0x5f4f73 in __gnu_cxx::new_allocator<char>::allocate(unsigned long,
void const*) /usr/include/c++/7.3.0/ext/new_allocator.h:111
#2 0x5f4f73 in std::allocator_traits<std::allocator<char>
>::allocate(std::allocator<char>&, unsigned long)
/usr/include/c++/7.3.0/bits/alloc_traits.h:436
#3 0x5f4f73 in std::_Vector_base<char, std::allocator<char>
>::_M_allocate(unsigned long) /usr/include/c++/7.3.0/bits/stl_vector.h:172
#4 0x5f4f73 in void std::vector<char, std::allocator<char>
>::_M_range_initialize<__gnu_cxx::__normal_iterator<char const*,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
> >(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, std::forward_iterator_tag)
/usr/include/c++/7.3.0/bits/stl_vector.h:1323
#5 0x5f4f73 in void std::vector<char, std::allocator<char>
>::_M_initialize_dispatch<__gnu_cxx::__normal_iterator<char const*,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
> >(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, std::__false_type)
/usr/include/c++/7.3.0/bits/stl_vector.h:1299
#6 0x5f4f73 in std::vector<char, std::allocator<char>
>::vector<__gnu_cxx::__normal_iterator<char const*,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
>, void>(__gnu_cxx::__normal_iterator<char const*,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
>, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, std::allocator<char> const&)
/usr/include/c++/7.3.0/bits/stl_vector.h:414
#7 0x5f4f73 in
doris::path_util::base_name(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)
/home/laiyingchun/ap_doris/be/src/util/path_util.cpp:79
#8 0x5da0fa in doris::TestPathUtil_BaseNameTest_Test::TestBody()
/home/laiyingchun/ap_doris/be/test/util/path_util_test.cpp:44
#9 0x6c6d54 in void
testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test,
void>(testing::Test*, void (testing::Test::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c6d54)
#10 0x6c15ad in void
testing::internal::HandleExceptionsInMethodIfSupported<testing::Test,
void>(testing::Test*, void (testing::Test::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c15ad)
#11 0x6a686f in testing::Test::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6a686f)
#12 0x6a70f1 in testing::TestInfo::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6a70f1)
#13 0x6a7748 in testing::TestCase::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6a7748)
#14 0x6ae2bc in testing::internal::UnitTestImpl::RunAllTests()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6ae2bc)
#15 0x6c7d6a in bool
testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl,
bool>(testing::internal::UnitTestImpl*, bool
(testing::internal::UnitTestImpl::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c7d6a)
#16 0x6c22f9 in bool
testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl,
bool>(testing::internal::UnitTestImpl*, bool
(testing::internal::UnitTestImpl::*)(), char const*)
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6c22f9)
#17 0x6acf93 in testing::UnitTest::Run()
(/home/laiyingchun/ap_doris/be/build/test/util/path_util_test+0x6acf93)
#18 0x5c9801 in RUN_ALL_TESTS()
/home/laiyingchun/ap_doris/thirdparty/installed/include/gtest/gtest.h:2233
#19 0x5c9801 in main
/home/laiyingchun/ap_doris/be/test/util/path_util_test.cpp:113
#20 0x7fec8e60f504 in __libc_start_main (/lib64/libc.so.6+0x22504)
SUMMARY: AddressSanitizer: heap-buffer-overflow
../../.././libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:225
in __interceptor_strlen
Shadow bytes around the buggy address:
0x0c047fff8120: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
0x0c047fff8130: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
0x0c047fff8140: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
0x0c047fff8150: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
0x0c047fff8160: fa fa 00 00 fa fa 00 fa fa fa fd fd fa fa 00 00
=>0x0c047fff8170: fa fa[01]fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8190: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff81a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff81b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff81c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==133070==ABORTING
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]