为什么 valgrind 为 libstdc++ 的 std::locale 报告错误?

Why is valgrind reporting errors for libstdc++'s std::locale?

本文关键字:locale 报告 错误 std valgrind libstdc++ 为什么      更新时间:2023-10-16

相关问题: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中的一个假阳性,在这里修复。