[libcxx-dev] Building a program with -D_LIBCPP_DEBUG=1 against a libc++ that is not itself built with that define

Stephan Bergmann via libcxx-dev libcxx-dev at lists.llvm.org
Fri Oct 29 08:09:06 PDT 2021


At least with recent LLVM 14 trunk,

> $ cat test.cc
> #include <string>
> int main() { "longlonglonglonglonglong" + std::to_string(0); }

> $ clang++ -stdlib=libc++ -D_LIBCPP_DEBUG=1 test.cc
> $ ./a.out
> Segmentation fault

at

> #0  std::__1::__libcpp_db::swap (this=0x7f3187f63318 <std::__1::__get_db()::db>, c1=<optimized out>, c2=0x7ffe55d37840) at src/debug.cpp:461
> #1  0x0000000000401744 in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string (this=0x7ffe55d37858, __str=...) at include/c++/v1/string:1991
> #2  0x0000000000401334 in std::__1::operator+<char, std::__1::char_traits<char>, std::__1::allocator<char> > (__lhs=0x403010 "longlonglonglonglonglong", __rhs=...) at include/c++/v1/string:4342
> #3  0x00000000004012a9 in main () at test.cc:2

because the std::string instance returned from std::to_string 
(implemented in libc++, not itself built with -D_LIBCPP_DEBUG=1) never 
called __libcpp_db::__insert_c(this), so __libcpp_db::swap doesn't find 
it in the __cbeg_/__cend_ table and then dereferences a null pointer.

I wonder how usable and mature -D_LIBCPP_DEBUG=1 is considered in 
practice.  Is this a well-known shortcoming (in which case it might be 
good to mention it explicitly in libcxx/docs/DesignDocs/DebugMode.rst, 
beyond just "Users should compile their whole program at the same 
debugging level."), or would it be worth filing a bug?



More information about the libcxx-dev mailing list