From 9bf6dc2eaf7b3252fb7db73ed8ead6f568bbb0ec Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Wed, 12 Aug 2015 19:24:50 +0200 Subject: [PATCH] error.h: Never call (para_)strerror() on osl errors. In the (inlined) function para_strerror() of error.h we compile in the call to osl_strerror() if and only if osl.h has been included (in which case the preprocessor macro _OSL_H is defined). This is wrong because an osl error code might well be passed to a function defined in a compilation unit which does not include osl.h. If this function calls para_strerror(), we will segfault or worse. We need to check at link time whether osl_strerror is a defined symbol rather than checking a preprocessor macro at compile time. Fortunately, a little linker fu helps us out here. This patch introduces a weak reference to osl_strerror to tell whether osl_strerror is defined. --- error.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/error.h b/error.h index 379d3732..49a57a5a 100644 --- a/error.h +++ b/error.h @@ -578,6 +578,8 @@ extern const char **para_errlist[]; /** Set the osl error bit for the given number. */ #define OSL_ERRNO_TO_PARA_ERROR(num) ((num) | (1 << OSL_ERROR_BIT)) + +static const char *weak_osl_strerror(int) __attribute__ ((weakref("osl_strerror"))); /** * Paraslash's version of strerror(3). * @@ -588,10 +590,10 @@ extern const char **para_errlist[]; _static_inline_ const char *para_strerror(int num) { assert(num > 0); -#ifdef _OSL_H - if (IS_OSL_ERROR(num)) - return osl_strerror(num & ~(1U << OSL_ERROR_BIT)); -#endif + if (IS_OSL_ERROR(num)) { + assert(weak_osl_strerror); + return weak_osl_strerror(num & ~(1U << OSL_ERROR_BIT)); + } if (IS_SYSTEM_ERROR(num)) return strerror(num & ~(1U << SYSTEM_ERROR_BIT)); return para_errlist[ERRNUM_TO_SS(num)][ERRNUM_TO_INDEX(num)]; -- 2.30.2