clang ++内存清理程序报告未初始化的值的使用 [英] clang++ memory sanitizer reports use-of-uninitialized-value
问题描述
此代码摘自 IncludeOS github页.我对其进行了一些修改,以便在不使用其他头文件的情况下进行编译.IncludeOS的 find
函数有点太冗长,因此我想简化一下.但是修改后,代码的行为与我预期的有所不同.
This code is taken from IncludeOS github page. I modified it a bit so that it compiles without other header files. find
function from IncludeOS is a bit too verbose, so I want to simplify it. But after modification, the code behaves differently from what I expected.
这里是一个简短的解释.此代码用于解析HTTP标头.标头字段是名称-值对.它表示为 vector< pair< string,string>>
. find
函数用于查找字段名称在标头中的位置,而 has_field
用于检查标头中是否存在特定的字段名称.
Here is a short explanation. This code is used to parse HTTP headers. Header fields are name-value pairs. It's represented as vector<pair<string, string>>
. find
function is used to find the location of a field name in the header, and has_field
is used to check whether a specific field name exists in the header.
在 main
函数中,四个元素被附加到字段中.在字段中找不到 6
,但是 has_field
返回true.
In main
function, four elements are appended to fields. six
shouldn't be found in fields.But has_field
returns true.
我尝试使用 gdb
跟踪错误.但是我迷失在产出之海中.我确实发现了一条有趣的消息.
I tried to track the error with gdb
. But I was lost in the sea of outputs. I did find a somewhat interesting message.
std :: __ uninitialized_copy< false> :: __ uninit_copy< __ gnu_cxx :: __ normal_iterator< std :: pair< std :: __ cxx11 :: basic_string< char,std :: char_traits< char> ;, std :: allocator>,std :: __ cxx11 :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>>const *,std :: vector< std :: pair< std :: __ cxx11 :: basic_string< char,std :: char_traits< char<,std :: allocator< char>>,std :: __ cxx11 :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>>,std :: allocator< std :: pair< std :: __ cxx11 :: basic_string< char,std :: char_traits< char<,std :: allocator< char>>,std :: __ cxx11 :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>>>> ;、 std :: pair< std :: __ cxx11 :: basic_string< char,std :: char_traits< char> ;, std :: allocator< char>>,std :: __ cxx11 :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>> *>(__first = {第一个=一个",第二个="1"},__ last =
std::__uninitialized_copy<false>::__uninit_copy<__gnu_cxx::__normal_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const*, std::vector<std::pair<std::__cxx11::basic_string<char, std::char_traits<char<, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char<, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >>>>, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*> (__first={first = "one", second = "1"}, __last=
{first =<错误读取变量:无法创建地址为0x0且长度为非零的惰性字符串.>,second ="},__result = 0x61bf00)
{first = <error reading variable: Cannot create a lazy string with address 0x0, and a non-zero length.>, second = ""}, __result=0x61bf00)
我使用了 clang
消毒剂来找出问题所在.仅内存清理程序显示有趣的报告.跑步
I used clang
sanitizer to find out what's wrong. Only memory sanitizer shows interesting reports. Running,
clang ++ -std = c ++ 17 -O1 -fsanitize = memory -fsanitize-memory-track-origins -fno-omit-frame-pointer main.cc
/a.out
报告
未初始化的值是通过在函数'_ZNSt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_EC2IRA6_KcRA2_S8_Lb1EEEOT_OT0_'的堆栈帧中分配"ref.tmp"而创建的.
Uninitialized value was created by an allocation of 'ref.tmp' in the stack frame of function '_ZNSt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_EC2IRA6_KcRA2_S8_Lb1EEEOT_OT0_'`.
但是,当优化级别设置为 -O3
时,什么都没有显示.
When optimization level is set to -O3
, however, nothing shows up.
#include <algorithm>
#include <iostream>
#include <vector>
#include <experimental/string_view>
using Headers = std::vector<std::pair<std::string, std::string>>;
using string_view = std::experimental::string_view;
Headers::const_iterator find(Headers fields, const string_view field) {
if (field.empty()) return fields.cend();
//-----------------------------------
return
std::find_if(fields.cbegin(), fields.cend(), [field](const auto _) {
return std::equal(_.first.cbegin(), _.first.cend(), field.cbegin(), field.cend(),
[](const auto a, const auto b) { return std::tolower(a) == std::tolower(b); });
});
}
bool has_field(Headers fields, const string_view field)
{
return find(fields, field) != fields.cend();
}
int main()
{
Headers fields;
fields.emplace_back("one", "1");
fields.emplace_back("two", "2");
fields.emplace_back("three", "3");
fields.emplace_back("four", "4");
std::string s = "six";
if (has_field(fields, s))
std::cout << s << " is in " << "fields" << std::endl;
return 0;
}
推荐答案
这可能是误报.llvm随附符号程序二进制文件,该二进制文件允许消毒程序输出行号.我已经通过以下最小示例重现了您的错误:
It's likely a false positive. llvm comes with the symbolizer binary, which allows the sanitizer to output line numbers. I've managed to reproduce your error with this minimal example:
1 #include <iostream>
2 #include <vector>
3
4 using Headers = std::vector<int>;
5
6 bool a(Headers fields) {
7 return true;
8 }
9
10 bool b(Headers fields)
11 {
12 return a(fields);
13 }
14
15 int main()
16 {
17 Headers fields;
18
19 if (b(fields)) {
20 std::cout << std::endl;
21 }
22
23 return 0;
24 }
在这两种情况下,堆栈跟踪声明 std :: endl
是罪魁祸首.为了使错误发生,必须发生以下神奇的事情:
In both cases, the stack trace claims std::endl
is the culprit. For the error to occur the following magical things have to occur:
- 输出
std :: endl
- 具有两个函数调用
如果我声明 a
通过引用获取 fields
,则错误消失;对于 b
,不能说相同的话.所有这些使我相信这是荒谬的,是一种误报.作为参考,这是带有行号的消毒剂输出:
If I declare a
to take fields
by reference, the error disappears; the same cannot be said for b
. All of this leads me to believe it's nonsensical and a false positive. For reference, here's the sanitizer output with line numbers:
Uninitialized bytes in __interceptor_memcmp at offset 192 inside [0x7fff18347610, 256)
==5724==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x7f8f663d94ab in std::ctype<char>::_M_widen_init() const (/lib64/libstdc++.so.6+0xb74ab)
#1 0x7f8f66435d17 in std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) (/lib64/libstdc++.so.6+0x113d17)
#2 0x4912ff in main test.cpp:20:15
#3 0x7f8f65415889 in __libc_start_main (/lib64/libc.so.6+0x20889)
#4 0x41a9b9 in _start (a.out+0x41a9b9)
Uninitialized value was created by an allocation of 'ref.tmp' in the stack frame of function '_ZNSt6vectorIiSaIiEEC2ERKS1_'
#0 0x491360 in std::vector<int, std::allocator<int> >::vector(std::vector<int, std::allocator<int> > const&) /usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7/bits/stl_vector.h:329
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/lib64/libstdc++.so.6+0xb74ab) in std::ctype<char>::_M_widen_init() const
Exiting
这篇关于clang ++内存清理程序报告未初始化的值的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!