libstdc++: Add error handler for <stacktrace>

Message ID 776a72a7-a6b8-3bd2-d758-821c70420ed9@hazardy.de
State Unresolved
Headers
Series libstdc++: Add error handler for <stacktrace> |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Björn Schäpers Nov. 29, 2022, 9:48 p.m. UTC
  -------- Weitergeleitete Nachricht --------
Betreff: [PATCH] libstdc++: Add error handler for <stacktrace>
Datum: Tue, 29 Nov 2022 22:41:07 +0100
Von: Björn Schäpers <gcc@hazardy.de>
An: gcc-patches@gc.gnu.org, libstdc++@gcc.gnu.org

From: Björn Schäpers <bjoern@hazardy.de>

Not providing an error handler results in a nullpointer dereference when
an error occurs.

libstdc++-v3/ChangeLog

	* include/std/stacktrace: Add __backtrace_error_handler and use
	it in all calls to libbacktrace.
---
  libstdc++-v3/include/std/stacktrace | 21 ++++++++++++++-------
  1 file changed, 14 insertions(+), 7 deletions(-)

      {
        static __glibcxx_backtrace_state* __state
-	= __glibcxx_backtrace_create_state(nullptr, 1, nullptr, nullptr);
+	= __glibcxx_backtrace_create_state(nullptr, 1,
+					   __backtrace_error_handler, nullptr);
        return __state;
      }
  @@ -192,7 +196,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
  	  return __function != nullptr;
        };
        const auto __state = _S_init();
-      if (::__glibcxx_backtrace_pcinfo(__state, _M_pc, +__cb, nullptr, &__data))
+      if (::__glibcxx_backtrace_pcinfo(__state, _M_pc, +__cb,
+				       __backtrace_error_handler, &__data))
  	return true;
        if (__desc && __desc->empty())
  	{
@@ -201,8 +206,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
  	      if (__symname)
  		*static_cast<_Data*>(__data)->_M_desc = _S_demangle(__symname);
  	  };
-	  if (::__glibcxx_backtrace_syminfo(__state, _M_pc, +__cb2, nullptr,
-					    &__data))
+	  if (::__glibcxx_backtrace_syminfo(__state, _M_pc, +__cb2,
+					    __backtrace_error_handler, &__data))
  	    return true;
  	}
        return false;
@@ -252,7 +257,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
  	if (auto __cb = __ret._M_prepare()) [[likely]]
  	  {
  	    auto __state = stacktrace_entry::_S_init();
-	    if (__glibcxx_backtrace_simple(__state, 1, __cb, nullptr,
+	    if (__glibcxx_backtrace_simple(__state, 1, __cb,
+					   __backtrace_error_handler,
  					   std::__addressof(__ret)))
  	      __ret._M_clear();
  	  }
@@ -270,7 +276,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
  	if (auto __cb = __ret._M_prepare()) [[likely]]
  	  {
  	    auto __state = stacktrace_entry::_S_init();
-	    if (__glibcxx_backtrace_simple(__state, __skip + 1, __cb, nullptr,
+	    if (__glibcxx_backtrace_simple(__state, __skip + 1, __cb,
+					   __backtrace_error_handler,
  					   std::__addressof(__ret)))
  	      __ret._M_clear();
  	  }
@@ -294,7 +301,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
  	  {
  	    auto __state = stacktrace_entry::_S_init();
  	    int __err = __glibcxx_backtrace_simple(__state, __skip + 1, __cb,
-						   nullptr,
+						   __backtrace_error_handler,
  						   std::__addressof(__ret));
  	    if (__err < 0)
  	      __ret._M_clear();
  

Patch

diff --git a/libstdc++-v3/include/std/stacktrace 
b/libstdc++-v3/include/std/stacktrace
index e7cbbee5638..b786441cbad 100644
--- a/libstdc++-v3/include/std/stacktrace
+++ b/libstdc++-v3/include/std/stacktrace
@@ -85,6 +85,9 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
   #define __cpp_lib_stacktrace 202011L
  +  inline void
+  __backtrace_error_handler(void*, const char*, int) {}
+
    // [stacktrace.entry], class stacktrace_entry
    class stacktrace_entry
    {
@@ -159,7 +162,8 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      _S_init()