diff --git a/libc/bionic/strerror.cpp b/libc/bionic/strerror.cpp index 036ec8da0..455dc5234 100644 --- a/libc/bionic/strerror.cpp +++ b/libc/bionic/strerror.cpp @@ -29,9 +29,17 @@ #include #include "ThreadLocalBuffer.h" +extern "C" const char* __strerror_lookup(int); + GLOBAL_INIT_THREAD_LOCAL_BUFFER(strerror); char* strerror(int error_number) { + // Just return the original constant in the easy cases. + char* result = const_cast(__strerror_lookup(error_number)); + if (result != NULL) { + return result; + } + LOCAL_INIT_THREAD_LOCAL_BUFFER(char*, strerror, NL_TEXTMAX); strerror_r(error_number, strerror_buffer, strerror_buffer_size); return strerror_buffer; diff --git a/libc/bionic/strerror_r.cpp b/libc/bionic/strerror_r.cpp index 92235a51d..a00ebe5f7 100644 --- a/libc/bionic/strerror_r.cpp +++ b/libc/bionic/strerror_r.cpp @@ -27,11 +27,25 @@ static const Pair _sys_error_strings[] = { { 0, NULL } }; +extern "C" const char* __strerror_lookup(int error_number) { + return __code_string_lookup(_sys_error_strings, error_number); +} + +static const Pair _sys_signal_strings[] = { +#define __BIONIC_SIGDEF(x,y,z) { y, z }, +#include + { 0, NULL } +}; + +extern "C" const char* __strsignal_lookup(int signal_number) { + return __code_string_lookup(_sys_signal_strings, signal_number); +} + int strerror_r(int error_number, char* buf, size_t buf_len) { int saved_errno = errno; size_t length; - const char* error_name = __code_string_lookup(_sys_error_strings, error_number); + const char* error_name = __strerror_lookup(error_number); if (error_name != NULL) { length = snprintf(buf, buf_len, "%s", error_name); } else { @@ -46,14 +60,8 @@ int strerror_r(int error_number, char* buf, size_t buf_len) { return 0; } -static const Pair _sys_signal_strings[] = { -#define __BIONIC_SIGDEF(x,y,z) { y, z }, -#include - { 0, NULL } -}; - extern "C" const char* __strsignal(int signal_number, char* buf, size_t buf_len) { - const char* signal_name = __code_string_lookup(_sys_signal_strings, signal_number); + const char* signal_name = __strsignal_lookup(signal_number); if (signal_name != NULL) { return signal_name; } diff --git a/libc/bionic/strsignal.cpp b/libc/bionic/strsignal.cpp index 1cbec9b5c..9b046d4f2 100644 --- a/libc/bionic/strsignal.cpp +++ b/libc/bionic/strsignal.cpp @@ -29,11 +29,18 @@ #include #include "ThreadLocalBuffer.h" +extern "C" const char* __strsignal_lookup(int); extern "C" const char* __strsignal(int, char*, size_t); GLOBAL_INIT_THREAD_LOCAL_BUFFER(strsignal); char* strsignal(int signal_number) { + // Just return the original constant in the easy cases. + char* result = const_cast(__strsignal_lookup(signal_number)); + if (result != NULL) { + return result; + } + LOCAL_INIT_THREAD_LOCAL_BUFFER(char*, strsignal, NL_TEXTMAX); return const_cast(__strsignal(signal_number, strsignal_buffer, strsignal_buffer_size)); }