From 2b818dbe445039bd87c2b1d001c233ebb034dec9 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 2 Jan 2022 23:40:31 +0900 Subject: [PATCH] Use strerror() instead of strerror_r() This should fix the build on Android. Fixes https://github.com/rui314/mold/issues/231 --- main.cc | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/main.cc b/main.cc index 665f89c041..a50d1b3033 100644 --- a/main.cc +++ b/main.cc @@ -7,16 +7,12 @@ namespace mold { std::string_view errno_string() { - static thread_local char buf[200]; -#if _GNU_SOURCE - // The GNU version of strerror_r() returns a char * and may completely ignore - // buf - return strerror_r(errno, buf, sizeof(buf)); -#else - // The POSIX.1-2001-compliant version of strerror_r() returns an int and - // writes the string into buf - return !strerror_r(errno, buf, sizeof(buf)) ? buf : "Unknown error"; -#endif + // There's a thread-safe version of strerror() (strerror_r()), but + // GNU and POSIX define that function differently. To avoid the mess, + // we simply use strerror() with a lock. + static std::mutex mu; + std::lock_guard lock(mu); + return strerror(errno); } #ifdef GIT_HASH