From bdb9639ee99b68a8c7541e78057e96fe6a2c62ed Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 11 Apr 2022 10:20:40 +0100 Subject: 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/testsuite/19_diagnostics/stacktrace/entry.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'libstdc++-v3/testsuite') diff --git a/libstdc++-v3/testsuite/19_diagnostics/stacktrace/entry.cc b/libstdc++-v3/testsuite/19_diagnostics/stacktrace/entry.cc index 0bbcabd8fae..a222c425b20 100644 --- a/libstdc++-v3/testsuite/19_diagnostics/stacktrace/entry.cc +++ b/libstdc++-v3/testsuite/19_diagnostics/stacktrace/entry.cc @@ -36,7 +36,8 @@ test_members() VERIFY( e1 != e2 ); VERIFY( e1.description() == e2.description() ); VERIFY( e1.source_file() == e2.source_file() ); - VERIFY( e1.source_line() != e2.source_line() ); + VERIFY( e1.source_line() == (__LINE__ - 5) ); + VERIFY( e2.source_line() == (__LINE__ - 5) ); std::stacktrace_entry e3 = []{ return std::stacktrace::current().at(0); @@ -44,10 +45,10 @@ test_members() VERIFY( e1 != e3 ); VERIFY( e1.description() != e3.description() ); VERIFY( e1.source_file() == e3.source_file() ); - VERIFY( e1.source_line() != e3.source_line() ); + VERIFY( e3.source_line() == (__LINE__ - 5) ); } int main() { - test_constexpr(); + test_members(); } -- cgit v1.2.3