簡體   English   中英

clang ++與g ++的-fsanitize = address的不同輸出

[英]different output for -fsanitize=address with clang++ vs g++

在以下代碼中,我遇到了問題。 當我給它一個仍然完全為空的向量時,代碼會崩潰,因為vector.size()-1不能為負,因此它會回繞。 由於向量為空,因此訪問container [0]無效。

using namespace std;

template<typename T, typename A>
std::string vec_to_python_list(
        const std::string& name,
        const vector<T, A>& container
        ) {
    std::stringstream stream(ios::out);
    stream << name << " = [" << '\n';
    for (typename vector<T, A>::size_type i = 0; i < container.size() - 1; i++)
        stream << "\t" << container[i] << ",\n";
    if (name.size() > 0)
        stream << "\t" << container[container.size() - 1] << "\n";
    stream << "]";
    return stream.str();
}

我的問題是關於它產生的輸出。

如果我在Ubuntu-16.04上使用g ++進行編譯

g++ -Wall -Wextra -std=c++14 -g -fsanitize=address -O0  -o test_kmeans test_kmeans.cpp

我獲得下一個有用的信息:

#0 0x402a56 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > vec_to_python_list<double, std::allocator<double> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<double, std::allocator<double> > const&) /home/hetepeperfan/programming/cpp/kmeans/test_kmeans.cpp:46
#1 0x401f07 in main /home/hetepeperfan/programming/cpp/kmeans/test_kmeans.cpp:66
#2 0x7f393881f82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#3 0x401ba8 in _start (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x401ba8)

但是如果我用clang ++編譯(仍然在Ubuntu-16.04上)

clang++ -Wall -Wextra -std=c++14 -g -fsanitize=address -O0  -o test_kmeans test_kmeans.cpp

我得到的有用結果較少:

#0 0x4eecae  (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x4eecae)
#1 0x4ee056  (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x4ee056)
#2 0x7f6ed883a82f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#3 0x419838  (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x419838)

我在做什么錯,以便帶有-fsanitize = address的g ++可以正常工作,而clang ++不會產生有用的結果,似乎未添加調試符號?

編輯調試符號似乎存在,因為使用gdb可以輕松地逐步執行代碼,而使用--tui gdb選項可以看到我的代碼,所以這不是問題。

安裝llvm-symbolizer。 還要將ASAN_SYMBOLIZER_PATH環境變量設置為類似

ASAN_SYMBOLIZER_PATH=/usr/lib/llvm-5.0/bin/llvm-symbolizer

llvm正在尋找名為llvm-symbolizer而不是llvm-symbolizer-3.8的可執行文件,這就是為什么環境變量必須指向llvm-symbolizer的文件名沒有版本號的原因。 如果您所有的符號化程序的文件名中都有版本號,請建立一個沒有版本號的符號鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM