From 154fa4d7ef23ee85e937ab6d1f1fab49dfb1872a Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Wed, 25 May 2011 18:47:32 +0000 Subject: [PATCH] perror: fix POSIX compliance and work with recent newlib change * cygtls.h (strerror_r_buf): New buffer. * errno.cc (strerror): Move guts... (_strerror_r): ...to new function demanded by newlib. (strerror_r): Don't clobber strerror buffer. (_user_strerror): Drop unused declaration. * tlsoffsets.h: Regenerate. --- winsup/cygwin/ChangeLog | 9 +++++ winsup/cygwin/cygtls.h | 1 + winsup/cygwin/errno.cc | 43 ++++++++++++++------ winsup/cygwin/tlsoffsets.h | 82 +++++++++++++++++++------------------- 4 files changed, 81 insertions(+), 54 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 238d7636b..8114f68df 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,12 @@ +2011-05-25 Eric Blake + + * cygtls.h (strerror_r_buf): New buffer. + * errno.cc (strerror): Move guts... + (_strerror_r): ...to new function demanded by newlib. + (strerror_r): Don't clobber strerror buffer. + (_user_strerror): Drop unused declaration. + * tlsoffsets.h: Regenerate. + 2011-05-25 Corinna Vinschen * init.cc (dll_entry): Reinstantiate wow64_test_stack_marker and diff --git a/winsup/cygwin/cygtls.h b/winsup/cygwin/cygtls.h index 6359e7c5f..a1642b1bc 100644 --- a/winsup/cygwin/cygtls.h +++ b/winsup/cygwin/cygtls.h @@ -110,6 +110,7 @@ struct _local_storage /* strerror errno.cc */ char strerror_buf[sizeof ("Unknown error -2147483648")]; + char strerror_r_buf[sizeof ("Unknown error -2147483648")]; /* times.cc */ char timezone_buf[20]; diff --git a/winsup/cygwin/errno.cc b/winsup/cygwin/errno.cc index aa311b7e2..f3b925ef2 100644 --- a/winsup/cygwin/errno.cc +++ b/winsup/cygwin/errno.cc @@ -360,8 +360,6 @@ seterrno (const char *file, int line) seterrno_from_win_error (file, line, GetLastError ()); } -extern char *_user_strerror _PARAMS ((int)); - static char * strerror_worker (int errnum) { @@ -373,22 +371,38 @@ strerror_worker (int errnum) return res; } -/* strerror: convert from errno values to error strings. Newlib's - strerror_r returns "" for unknown values, so we override it to - provide a nicer thread-safe result string and set errno. */ +/* Newlib requires this override for perror and friends to avoid + clobbering strerror() buffer, without having to differentiate + between strerror_r signatures. This function is intentionally not + exported, so that only newlib can use it. */ extern "C" char * -strerror (int errnum) +_strerror_r (struct _reent *, int errnum, int internal, int *errptr) { char *errstr = strerror_worker (errnum); if (!errstr) { - __small_sprintf (errstr = _my_tls.locals.strerror_buf, "Unknown error %d", - errnum); - errno = _impure_ptr->_errno = EINVAL; + errstr = internal ? _my_tls.locals.strerror_r_buf + : _my_tls.locals.strerror_buf; + __small_sprintf (errstr, "Unknown error %d", errnum); + if (errptr) + *errptr = EINVAL; } return errstr; } +/* strerror: convert from errno values to error strings. Newlib's + strerror_r returns "" for unknown values, so we override it to + provide a nicer thread-safe result string and set errno. */ +extern "C" char * +strerror (int errnum) +{ + int error = 0; + char *result = _strerror_r (NULL, errnum, 0, &error); + if (error) + set_errno (error); + return result; +} + /* Newlib's provides declarations for two strerror_r variants, according to preprocessor feature macros. However, it returns "" instead of "Unknown error ...", so we override both @@ -396,10 +410,13 @@ strerror (int errnum) extern "C" char * strerror_r (int errnum, char *buf, size_t n) { - char *error = strerror (errnum); - if (strlen (error) >= n) - return error; - return strcpy (buf, error); + int error = 0; + char *errstr = _strerror_r (NULL, errnum, 1, &error); + if (error) + set_errno (error); + if (strlen (errstr) >= n) + return errstr; + return strcpy (buf, errstr); } extern "C" int diff --git a/winsup/cygwin/tlsoffsets.h b/winsup/cygwin/tlsoffsets.h index 4e459dfdb..3ef7963c7 100644 --- a/winsup/cygwin/tlsoffsets.h +++ b/winsup/cygwin/tlsoffsets.h @@ -1,6 +1,6 @@ //;# autogenerated: Do not edit. -//; $tls::sizeof__cygtls = 3984; +//; $tls::sizeof__cygtls = 4012; //; $tls::func = -12700; //; $tls::pfunc = 0; //; $tls::saved_errno = -12696; @@ -37,26 +37,26 @@ //; $tls::p__dontuse = 412; //; $tls::locals = -11200; //; $tls::plocals = 1500; -//; $tls::_ctinfo = -9800; -//; $tls::p_ctinfo = 2900; -//; $tls::andreas = -9796; -//; $tls::pandreas = 2904; -//; $tls::wq = -9792; -//; $tls::pwq = 2908; -//; $tls::sig = -9764; -//; $tls::psig = 2936; -//; $tls::incyg = -9760; -//; $tls::pincyg = 2940; -//; $tls::spinning = -9756; -//; $tls::pspinning = 2944; -//; $tls::stacklock = -9752; -//; $tls::pstacklock = 2948; -//; $tls::stackptr = -9748; -//; $tls::pstackptr = 2952; -//; $tls::stack = -9744; -//; $tls::pstack = 2956; -//; $tls::initialized = -8720; -//; $tls::pinitialized = 3980; +//; $tls::_ctinfo = -9772; +//; $tls::p_ctinfo = 2928; +//; $tls::andreas = -9768; +//; $tls::pandreas = 2932; +//; $tls::wq = -9764; +//; $tls::pwq = 2936; +//; $tls::sig = -9736; +//; $tls::psig = 2964; +//; $tls::incyg = -9732; +//; $tls::pincyg = 2968; +//; $tls::spinning = -9728; +//; $tls::pspinning = 2972; +//; $tls::stacklock = -9724; +//; $tls::pstacklock = 2976; +//; $tls::stackptr = -9720; +//; $tls::pstackptr = 2980; +//; $tls::stack = -9716; +//; $tls::pstack = 2984; +//; $tls::initialized = -8692; +//; $tls::pinitialized = 4008; //; __DATA__ #define tls_func (-12700) @@ -95,23 +95,23 @@ #define tls_p__dontuse (412) #define tls_locals (-11200) #define tls_plocals (1500) -#define tls__ctinfo (-9800) -#define tls_p_ctinfo (2900) -#define tls_andreas (-9796) -#define tls_pandreas (2904) -#define tls_wq (-9792) -#define tls_pwq (2908) -#define tls_sig (-9764) -#define tls_psig (2936) -#define tls_incyg (-9760) -#define tls_pincyg (2940) -#define tls_spinning (-9756) -#define tls_pspinning (2944) -#define tls_stacklock (-9752) -#define tls_pstacklock (2948) -#define tls_stackptr (-9748) -#define tls_pstackptr (2952) -#define tls_stack (-9744) -#define tls_pstack (2956) -#define tls_initialized (-8720) -#define tls_pinitialized (3980) +#define tls__ctinfo (-9772) +#define tls_p_ctinfo (2928) +#define tls_andreas (-9768) +#define tls_pandreas (2932) +#define tls_wq (-9764) +#define tls_pwq (2936) +#define tls_sig (-9736) +#define tls_psig (2964) +#define tls_incyg (-9732) +#define tls_pincyg (2968) +#define tls_spinning (-9728) +#define tls_pspinning (2972) +#define tls_stacklock (-9724) +#define tls_pstacklock (2976) +#define tls_stackptr (-9720) +#define tls_pstackptr (2980) +#define tls_stack (-9716) +#define tls_pstack (2984) +#define tls_initialized (-8692) +#define tls_pinitialized (4008) -- 2.43.5