libstdc++: Improve behaviour of std::stacktrace::current
authorJonathan Wakely <jwakely@redhat.com>
Mon, 11 Apr 2022 09:20:40 +0000 (10:20 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 11 Apr 2022 16:59:08 +0000 (17:59 +0100)
commitbdb9639ee99b68a8c7541e78057e96fe6a2c62ed
treeac012f710ff6d4f7bc6dec67892fa61c0d9bfebc
parentab0f04e4df1b7b312a4c9fa9b4d675778a0bae86
libstdc++: Improve behaviour of std::stacktrace::current

This prevents inlining the current() function to guarantee that it is
present in the stacktrace, then tells libbacktrace to skip that frame.

To avoid overflow in the int argument to __glibcxx_backtrace_simple, we
need to check if the skip parameter exceeds INT_MAX (which is possible
for 16-bit targets where short and int have the same width). We also
need to limit the size of the returned value to the max_depth parameter,
which was missing previously.

This also fixes basic_stacktrace::max_size() to not exceed the maximum
size supported by the allocator, which might be smaller than the maximum
value of size_type.

libstdc++-v3/ChangeLog:

* include/std/stacktrace (basic_stacktrace::current): Duplicate
implementation into each overload. Add noinline attribute and
skip current frame.
(basic_stacktrace::max_size()): Call _Impl::_S_max_size.
(basic_stacktrace::_S_curr_cb()): New function defining lambda.
(basic_stacktrace::_Impl::_S_max_size): New function defining
maximum size in terms of allocator and size_type.
(basic_stacktrace::_Impl::_M_allocate): Check against
max_size().
* testsuite/19_diagnostics/stacktrace/entry.cc: Call function
for non-constexpr checks. Check line number is correct.
libstdc++-v3/include/std/stacktrace
libstdc++-v3/testsuite/19_diagnostics/stacktrace/entry.cc