为什么 valgrind 为 libstdc++ 的 std::locale 报告错误?
Why is valgrind reporting errors for libstdc++'s std::locale?
相关问题:wifstream与imbue,locale产生valgrind错误
我使用的是cppreference的(可能有缺陷的)例子,特别是他们imbue
页面上的例子。在coliru在线编译器上使用命令行:
clang++ -std=c++14 -stdlib=libstdc++ -O3 -Wall -Wextra -pedantic-errors
-pthread main.cpp && valgrind ./a.out
以下测试用例会产生这样的错误(除非我声明"没有错误"):
==5421== Invalid read of size 8
==5421== at 0x590CBC0: wcscmp (wcscmp.S:208)
==5421== by 0x4EAC174: std::moneypunct<wchar_t, false>::~moneypunct() (monetary_members.cc:927)
==5421== by 0x4EAC1D8: std::moneypunct<wchar_t, false>::~moneypunct() (monetary_members.cc:932)
==5421== by 0x4EA1695: std::locale::_Impl::~_Impl() (locale_classes.h:412)
==5421== by 0x4EA17D8: std::locale::~locale() (locale_classes.h:521)
==5421== by 0x400955: main (in /tmp/1412433400.2497/a.out)
==5421== Address 0x5c2e0b8 is 0 bytes after a block of size 8 alloc'd
==5421== at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5421== by 0x4EABE61: std::moneypunct<wchar_t, false>::_M_initialize_moneypunct(__locale_struct*, char const*) (monetary_members.cc:847)
==5421== by 0x4EA3CD7: std::locale::_Impl::_Impl(char const*, unsigned long) (locale_facets_nonio.h:993)
==5421== by 0x4EA406A: std::locale::locale(char const*) (localename.cc:42)
==5421== by 0x40094D: main (in /tmp/1412433400.2497/a.out)
cppreference的imbue
示例:
#include <iostream>
#include <sstream>
#include <locale>
int main()
{
std::istringstream iss;
iss.imbue(std::locale("en_US.UTF8"));
std::cout << "Current locale: " << iss.getloc().name() << 'n';
iss.imbue(std::locale());
std::cout << "Global locale : " << iss.getloc().name() << 'n';
}
libstdc++ - errors
libc++ - no errors
在顶部被质疑的链接的简化示例:
#include <iostream>
#include <locale>
int main (int argc, char **argv) {
try {
std::locale * l1 = new std::locale("de_DE.UTF-8");
delete l1;
std::locale l2("de_DE.UTF-8");
} catch(...) {
return 0;
}
return 0;
};
libstdc++ - errors
libc++ - no errors
上面问题的简化示例中的链接错误报告:
#include <wchar.h>
void foo(int)
{
}
int main()
{
wchar_t *a=new wchar_t[2], *b=new wchar_t[2];
size_t j;
a[0]=b[0]='A';
a[1]=b[1]=0;
foo(wcscmp(a, b));
delete[] a;
delete[] b;
return 0;
}
libstdc++ - no errors
libc++ - no errors
为了完整性,我添加了错误报告的测试用例,尽管它们不会产生错误。Coliru的valgrind版本是3.7.0,链接线程中的OP提到升级到3.8.1,但仍收到错误。我现在没有在Linux机器上,所以我自己无法测试它。如果有什么不同的话,下面是glibc输出:
GNU C Library (Ubuntu EGLIBC 2.15-0ubuntu10.7) stable release version 2.15, by Roland McGrath et al.
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.6.3.
Compiled on a Linux 3.2.60 system on 2014-08-28.
Available extensions:
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.debian.org/Bugs/>.
虫子在哪里?cpprreference的示例、valgrind或libstdc++?
这是Valgrind中的一个假阳性,在这里修复。
相关文章:
- Android NDK传感器向事件队列报告奇怪的间隔
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- 使用std::source_location报告错误的最佳实践
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 当用户超过按钮点击限制时报告
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么cudaMemGetInfo报告设备内存总量的变化
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 内存清理程序报告全局对象构造中未初始化值的使用
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- 在 Linux 中使用 ioctl() 获取隐藏功能报告时,零字节消失
- Valgrind 在 std::string::swap 中报告 SIGILL
- DRD 报告"conflicting load" std::mutex::lock 上的错误
- 与 boost::locale 关于"ß"大写的例外行为混淆
- 柯南,CMake.test()生成XML报告
- 如何构建一个异常类来报告C++中的哪些文件和行号?
- Valgrind 在 QThread::start() 上报告内存泄漏
- 我是否访问了已释放的内存,或者在这种情况下DrMemory报告不正确?
- 瓦尔格林德报告在 =带有嵌套shared_ptrs的运算符上的读取错误
- 为什么 valgrind 为 libstdc++ 的 std::locale 报告错误?