diff mbox series

libstdc++: Handle strerror returning null

Message ID 20240731132514.56823-1-jwakely@redhat.com
State New
Headers show
Series libstdc++: Handle strerror returning null | expand

Commit Message

Jonathan Wakely July 31, 2024, 1:24 p.m. UTC
As discussed a couple of weeks ago, I'm going to push this.

Tested x86_64-linux (where this #else isn't even used, but I checked it
does at least compile when the #if isn't true).

-- >8 --

The linux man page for strerror says that some systems return NULL for
an unknown error number. That violates the C and POSIX standards, but we
can esily handle it to avoid a segfault.

libstdc++-v3/ChangeLog:

	* src/c++11/system_error.cc (strerror_string): Handle
	non-conforming NULL return from strerror.
---
 libstdc++-v3/src/c++11/system_error.cc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/src/c++11/system_error.cc b/libstdc++-v3/src/c++11/system_error.cc
index d01451ba1ef..38bc0446110 100644
--- a/libstdc++-v3/src/c++11/system_error.cc
+++ b/libstdc++-v3/src/c++11/system_error.cc
@@ -110,7 +110,11 @@  namespace
 #else
   string strerror_string(int err)
   {
-    return strerror(err); // XXX Not thread-safe.
+    auto str = strerror(err); // XXX Not thread-safe.
+    if (str) [[__likely__]]
+      return str;
+    // strerror should not return NULL, but some implementations do.
+    return "Unknown error";
   }
 #endif