[gcc r16-4450] libstdc++: Remove invalid entry from the end of std::stacktrace

Jonathan Wakely redi@gcc.gnu.org
Wed Oct 15 21:00:34 GMT 2025


https://gcc.gnu.org/g:6c272ca18b5226fe850049409ae5e36b80cf879b

commit r16-4450-g6c272ca18b5226fe850049409ae5e36b80cf879b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Oct 15 14:59:20 2025 +0100

    libstdc++: Remove invalid entry from the end of std::stacktrace
    
    The backtrace_simple function seems to consistently invoke the callback
    with an invalid -1UL value as the last entry, which seems to come from
    _Unwind_Backtrace. The glibc backtrace(3) function has a special case to
    not include that final invalid address, but libbacktrace doesn't seem to
    handle it. Do so in std::stacktrace::current() instead.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/stacktrace (basic_stacktrace::current): Call
            _M_trim before returning.
            (basic_stacktrace::_M_trim): New member function.

Diff:
---
 libstdc++-v3/include/std/stacktrace | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/libstdc++-v3/include/std/stacktrace b/libstdc++-v3/include/std/stacktrace
index 01e18ba171c0..b9e260e19f89 100644
--- a/libstdc++-v3/include/std/stacktrace
+++ b/libstdc++-v3/include/std/stacktrace
@@ -208,6 +208,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  {
 	    if (_S_current(__cb, std::__addressof(__ret)))
 	      __ret._M_clear();
+	    else
+	      __ret._M_trim();
 	  }
 	return __ret;
       }
@@ -224,6 +226,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  {
 	    if (_S_current(__cb, std::__addressof(__ret), __skip))
 	      __ret._M_clear();
+	    else
+	      __ret._M_trim();
 	  }
 
 	return __ret;
@@ -260,6 +264,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		      }
 		  }
 	      }
+	    else
+	      __ret._M_trim();
 	  }
 	return __ret;
       }
@@ -651,6 +657,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	}
       };
 
+      void
+      _M_trim() noexcept
+      {
+	// libbacktrace adds an invalid -1UL entry at the end, remove it.
+	if (!empty() && !end()[-1])
+	  _M_impl._M_resize(size() - 1, _M_alloc);
+      }
+
       [[no_unique_address]] allocator_type  _M_alloc{};
 
       _Impl _M_impl{};


More information about the Libstdc++-cvs mailing list