diff --git a/src/io/fopen_s.c b/src/io/fopen_s.c index c01912aa..0bedaf41 100644 --- a/src/io/fopen_s.c +++ b/src/io/fopen_s.c @@ -79,6 +79,7 @@ EXPORT errno_t fopen_s(FILE *restrict *restrict streamptr, const char *restrict filename, const char *restrict mode) { + int l_errno; if (unlikely(streamptr == NULL)) { invoke_safe_str_constraint_handler("fopen_s: streamptr is null", NULL, ESNULLP); @@ -97,6 +98,7 @@ EXPORT errno_t fopen_s(FILE *restrict *restrict streamptr, return ESNULLP; } + l_errno = errno; errno = 0; *streamptr = fopen(filename, mode); @@ -106,6 +108,8 @@ EXPORT errno_t fopen_s(FILE *restrict *restrict streamptr, invoke_safe_str_constraint_handler(errstr, NULL, errno); return errno; } + if (0 == errno) + errno = l_errno; return EOK; } diff --git a/src/io/fprintf_s.c b/src/io/fprintf_s.c index 954718a8..95c45f66 100644 --- a/src/io/fprintf_s.c +++ b/src/io/fprintf_s.c @@ -72,6 +72,7 @@ EXPORT int fprintf_s(FILE *restrict stream, const char *restrict fmt, ...) { int ret; const char *p; out_fct_wrap_type wrap; + int l_errno; if (unlikely(stream == NULL)) { invoke_safe_str_constraint_handler("fprintf_s: stream is null", NULL, @@ -92,6 +93,7 @@ EXPORT int fprintf_s(FILE *restrict stream, const char *restrict fmt, ...) { } } + l_errno = errno; errno = 0; wrap.arg = stream; va_start(ap, fmt); @@ -103,6 +105,8 @@ EXPORT int fprintf_s(FILE *restrict stream, const char *restrict fmt, ...) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, -ret); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/freopen_s.c b/src/io/freopen_s.c index 19b6b5ba..643c5d24 100644 --- a/src/io/freopen_s.c +++ b/src/io/freopen_s.c @@ -83,6 +83,7 @@ EXPORT errno_t freopen_s(FILE *restrict *restrict newstreamptr, const char *restrict filename, const char *restrict mode, FILE *restrict stream) { + int l_errno; if (unlikely(newstreamptr == NULL)) { invoke_safe_str_constraint_handler("freopen_s: newstreamptr is null", NULL, ESNULLP); @@ -101,6 +102,7 @@ EXPORT errno_t freopen_s(FILE *restrict *restrict newstreamptr, return ESNULLP; } + l_errno = errno; errno = 0; *newstreamptr = freopen(filename, mode, stream); @@ -110,6 +112,8 @@ EXPORT errno_t freopen_s(FILE *restrict *restrict newstreamptr, invoke_safe_str_constraint_handler(errstr, NULL, errno); return errno; } + if (0 == errno) + errno = l_errno; return EOK; } diff --git a/src/io/fscanf_s.c b/src/io/fscanf_s.c index 11fc294a..2e4c740c 100644 --- a/src/io/fscanf_s.c +++ b/src/io/fscanf_s.c @@ -86,6 +86,7 @@ any of the arguments corresponding to %s is a null pointer. EXPORT int fscanf_s(FILE *restrict stream, const char *restrict fmt, ...) { va_list ap; int ret; + int l_errno; #if defined(HAVE_STRSTR) char *p; #endif @@ -126,6 +127,7 @@ EXPORT int fscanf_s(FILE *restrict stream, const char *restrict fmt, ...) { } #endif + l_errno = errno; errno = 0; va_start(ap, fmt); ret = vfscanf(stream, fmt, ap); @@ -136,6 +138,8 @@ EXPORT int fscanf_s(FILE *restrict stream, const char *restrict fmt, ...) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/gets_s.c b/src/io/gets_s.c index 96ff8c06..0ccb0542 100644 --- a/src/io/gets_s.c +++ b/src/io/gets_s.c @@ -109,6 +109,7 @@ EXPORT char *_gets_s_chk(char *restrict dest, rsize_t dmax, #endif { char *ret; + int l_errno; if (unlikely(dest == NULL)) { invoke_safe_str_constraint_handler("gets_s: dest is null", NULL, @@ -145,6 +146,7 @@ EXPORT char *_gets_s_chk(char *restrict dest, rsize_t dmax, } } + l_errno = errno; errno = 0; ret = fgets(dest, dmax + 1, stdin); @@ -171,6 +173,8 @@ EXPORT char *_gets_s_chk(char *restrict dest, rsize_t dmax, #endif } } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/scanf_s.c b/src/io/scanf_s.c index 95a4b8c4..50a5a672 100644 --- a/src/io/scanf_s.c +++ b/src/io/scanf_s.c @@ -87,6 +87,7 @@ any of the arguments corresponding to %s is a null pointer. EXPORT int scanf_s(const char *restrict fmt, ...) { va_list ap; int ret; + int l_errno; #if defined(HAVE_STRSTR) char *p; #endif @@ -120,6 +121,7 @@ EXPORT int scanf_s(const char *restrict fmt, ...) { } #endif + l_errno = errno; errno = 0; va_start(ap, fmt); ret = vscanf(fmt, ap); @@ -130,6 +132,8 @@ EXPORT int scanf_s(const char *restrict fmt, ...) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/sscanf_s.c b/src/io/sscanf_s.c index d25fb9ae..a180248a 100644 --- a/src/io/sscanf_s.c +++ b/src/io/sscanf_s.c @@ -89,6 +89,7 @@ EXPORT int sscanf_s(const char *restrict buffer, const char *restrict fmt, ...) { va_list ap; int ret; + int l_errno; #if defined(HAVE_STRSTR) char *p; #endif @@ -129,6 +130,7 @@ EXPORT int sscanf_s(const char *restrict buffer, const char *restrict fmt, } #endif + l_errno = errno; errno = 0; va_start(ap, fmt); ret = vsscanf(buffer, fmt, ap); @@ -139,6 +141,8 @@ EXPORT int sscanf_s(const char *restrict buffer, const char *restrict fmt, strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/tmpfile_s.c b/src/io/tmpfile_s.c index ea54be35..8118aa17 100644 --- a/src/io/tmpfile_s.c +++ b/src/io/tmpfile_s.c @@ -80,6 +80,7 @@ EXPORT errno_t tmpfile_s(FILE *restrict *restrict streamptr) { static int count = 0; + int l_errno; if (unlikely(streamptr == NULL)) { invoke_safe_str_constraint_handler("tmpfile_s: streamptr is null", NULL, @@ -95,6 +96,7 @@ EXPORT errno_t tmpfile_s(FILE *restrict *restrict streamptr) { return ESLEMAX; } + l_errno = errno; errno = 0; *streamptr = tmpfile(); @@ -104,6 +106,8 @@ EXPORT errno_t tmpfile_s(FILE *restrict *restrict streamptr) { invoke_safe_str_constraint_handler(errstr, NULL, errno); return errno; } + if (0 == errno) + errno = l_errno; return EOK; } diff --git a/src/io/vfprintf_s.c b/src/io/vfprintf_s.c index 6e0cdb34..21ba4f00 100644 --- a/src/io/vfprintf_s.c +++ b/src/io/vfprintf_s.c @@ -71,6 +71,7 @@ EXPORT int vfprintf_s(FILE *restrict stream, const char *restrict fmt, va_list ap) { int ret; + int l_errno; const char *p; out_fct_wrap_type wrap; @@ -100,6 +101,7 @@ EXPORT int vfprintf_s(FILE *restrict stream, const char *restrict fmt, } } + l_errno = errno; errno = 0; #if 0 ret = vfprintf(stream, fmt, ap); @@ -118,6 +120,8 @@ EXPORT int vfprintf_s(FILE *restrict stream, const char *restrict fmt, invoke_safe_str_constraint_handler(errstr, NULL, -ret); } #endif + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/vfscanf_s.c b/src/io/vfscanf_s.c index 49630b6f..f19dd22e 100644 --- a/src/io/vfscanf_s.c +++ b/src/io/vfscanf_s.c @@ -89,6 +89,7 @@ EXPORT int vfscanf_s(FILE *restrict stream, const char *restrict fmt, char *p; #endif int ret; + int l_errno; if (unlikely(stream == NULL)) { invoke_safe_str_constraint_handler("vfscanf_s: stream is null", NULL, @@ -126,6 +127,7 @@ EXPORT int vfscanf_s(FILE *restrict stream, const char *restrict fmt, } #endif + l_errno = errno; errno = 0; ret = vfscanf(stream, fmt, ap); @@ -134,6 +136,8 @@ EXPORT int vfscanf_s(FILE *restrict stream, const char *restrict fmt, strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/vprintf_s.c b/src/io/vprintf_s.c index 7c660f6e..d75dccd3 100644 --- a/src/io/vprintf_s.c +++ b/src/io/vprintf_s.c @@ -67,6 +67,7 @@ EXPORT int vprintf_s(const char *restrict fmt, va_list ap) { int ret; + int l_errno; const char *p; if (unlikely(fmt == NULL)) { @@ -84,6 +85,7 @@ EXPORT int vprintf_s(const char *restrict fmt, va_list ap) { } } + l_errno = errno; errno = 0; ret = vprintf(fmt, ap); @@ -92,6 +94,8 @@ EXPORT int vprintf_s(const char *restrict fmt, va_list ap) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, -ret); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/vscanf_s.c b/src/io/vscanf_s.c index db999493..d57fdcd5 100644 --- a/src/io/vscanf_s.c +++ b/src/io/vscanf_s.c @@ -89,6 +89,7 @@ EXPORT int vscanf_s(const char *restrict fmt, va_list ap) { char *p; #endif int ret; + int l_errno; if (unlikely(fmt == NULL)) { invoke_safe_str_constraint_handler("vscanf_s: fmt is null", NULL, @@ -119,6 +120,7 @@ EXPORT int vscanf_s(const char *restrict fmt, va_list ap) { } #endif + l_errno = errno; errno = 0; ret = vscanf(fmt, ap); @@ -127,6 +129,8 @@ EXPORT int vscanf_s(const char *restrict fmt, va_list ap) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/io/vsscanf_s.c b/src/io/vsscanf_s.c index b133af34..75f6be96 100644 --- a/src/io/vsscanf_s.c +++ b/src/io/vsscanf_s.c @@ -91,6 +91,7 @@ EXPORT int vsscanf_s(const char *restrict buffer, const char *restrict fmt, char *p; #endif int ret; + int l_errno; if (unlikely(buffer == NULL)) { invoke_safe_str_constraint_handler("vsscanf_s: buffer is null", NULL, @@ -128,6 +129,7 @@ EXPORT int vsscanf_s(const char *restrict buffer, const char *restrict fmt, } #endif + l_errno = errno; errno = 0; ret = vsscanf(buffer, fmt, ap); @@ -136,6 +138,8 @@ EXPORT int vsscanf_s(const char *restrict buffer, const char *restrict fmt, strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/misc/bsearch_s.c b/src/misc/bsearch_s.c index f65ce7f1..3d72d69f 100644 --- a/src/misc/bsearch_s.c +++ b/src/misc/bsearch_s.c @@ -118,6 +118,7 @@ _bsearch_s_chk(const void *key, const void *base, rsize_t nmemb, rsize_t size, void *context, const size_t basebos) #endif { + int l_errno; if (likely(nmemb != 0)) { if (unlikely(key == NULL || base == NULL || compar == NULL)) { invoke_safe_mem_constraint_handler( @@ -153,6 +154,7 @@ _bsearch_s_chk(const void *key, const void *base, rsize_t nmemb, rsize_t size, #endif } + l_errno = errno; errno = 0; while (nmemb > 0) { @@ -169,6 +171,8 @@ _bsearch_s_chk(const void *key, const void *base, rsize_t nmemb, rsize_t size, nmemb -= nmemb / 2; } } + if (0 == errno) + errno = l_errno; return NULL; } diff --git a/src/os/getenv_s.c b/src/os/getenv_s.c index 7c0bebee..17d0289d 100644 --- a/src/os/getenv_s.c +++ b/src/os/getenv_s.c @@ -102,6 +102,7 @@ EXPORT errno_t _getenv_s_chk(size_t *restrict len, char *restrict dest, { const char *buf; size_t len1; + int l_errno; if (likely(dest)) { if (destbos == BOS_UNKNOWN) { @@ -142,6 +143,7 @@ EXPORT errno_t _getenv_s_chk(size_t *restrict len, char *restrict dest, return ESNULLP; } + l_errno = errno; errno = 0; #ifdef HAVE_SECURE_GETENV buf = secure_getenv(name); @@ -178,6 +180,8 @@ EXPORT errno_t _getenv_s_chk(size_t *restrict len, char *restrict dest, if (dest) strcpy_s(dest, dmax, buf); } + if (0 == errno) + errno = l_errno; return EOK; } diff --git a/src/str/strtok_s.c b/src/str/strtok_s.c index 5a019f6f..1ac892a8 100644 --- a/src/str/strtok_s.c +++ b/src/str/strtok_s.c @@ -185,6 +185,7 @@ EXPORT char *_strtok_s_chk(char *restrict dest, rsize_t *restrict dmaxp, rsize_t dlen; rsize_t slen; rsize_t dmax; + int l_errno; const char *orig_dest = dest; if (unlikely(dmaxp == NULL)) { @@ -256,6 +257,7 @@ EXPORT char *_strtok_s_chk(char *restrict dest, rsize_t *restrict dmaxp, */ dlen = *dmaxp; ptoken = NULL; + l_errno = errno; errno = 0; while (*dest != '\0' && !ptoken) { @@ -304,6 +306,8 @@ EXPORT char *_strtok_s_chk(char *restrict dest, rsize_t *restrict dmaxp, */ if (ptoken == NULL) { *dmaxp = dlen; + if (0 == errno) + errno = l_errno; return (ptoken); } @@ -357,6 +361,8 @@ EXPORT char *_strtok_s_chk(char *restrict dest, rsize_t *restrict dmaxp, dlen--; } + if (0 == errno) + errno = l_errno; *dmaxp = dlen; return (ptoken); } diff --git a/src/str/vsnprintf_s.c b/src/str/vsnprintf_s.c index fa53ab42..e0388d84 100644 --- a/src/str/vsnprintf_s.c +++ b/src/str/vsnprintf_s.c @@ -1480,6 +1480,7 @@ EXPORT int _vsnprintf_s_chk(char *restrict dest, rsize_t dmax, { const char *p; int ret; + int l_errno; if (unlikely(dest == NULL || fmt == NULL)) { invoke_safe_str_constraint_handler("vsnprintf_s: dest/fmt is null", @@ -1514,6 +1515,7 @@ EXPORT int _vsnprintf_s_chk(char *restrict dest, rsize_t dmax, } } + l_errno = errno; errno = 0; ret = safec_vsnprintf_s(safec_out_buffer, "vsnprintf_s", dest, dmax, fmt, ap); @@ -1544,5 +1546,7 @@ EXPORT int _vsnprintf_s_chk(char *restrict dest, rsize_t dmax, *dest = '\0'; #endif } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/str/vsprintf_s.c b/src/str/vsprintf_s.c index 69095c4b..0c7e9c30 100644 --- a/src/str/vsprintf_s.c +++ b/src/str/vsprintf_s.c @@ -103,6 +103,8 @@ EXPORT int _vsprintf_s_chk(char *restrict dest, const rsize_t dmax, #endif { int ret; + int l_errno = errno; + errno = 0; ret = _vsnprintf_s_chk(dest, dmax, destbos, fmt, ap); if (unlikely(dmax && ret >= (int)dmax) @@ -111,12 +113,11 @@ EXPORT int _vsprintf_s_chk(char *restrict dest, const rsize_t dmax, #endif ) { handle_error(dest, dmax, "vsprintf_s: len exceeds dmax", ESNOSPC); -#ifdef HAVE_MINGW32 - errno = 0; -#endif return -ESNOSPC; /* different to the standard (=0), but like all other implementations */ } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/fwprintf_s.c b/src/wchar/fwprintf_s.c index 12055102..3ffc9e1a 100644 --- a/src/wchar/fwprintf_s.c +++ b/src/wchar/fwprintf_s.c @@ -80,6 +80,7 @@ EXPORT int fwprintf_s(FILE *restrict stream, const wchar_t *restrict fmt, ...) { wchar_t *p; va_list ap; int ret; + int l_errno; if (unlikely(fmt == NULL)) { invoke_safe_str_constraint_handler("fwprintf_s: fmt is null", NULL, @@ -109,6 +110,7 @@ EXPORT int fwprintf_s(FILE *restrict stream, const wchar_t *restrict fmt, ...) { #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; va_start(ap, fmt); ret = vfwprintf(stream, fmt, ap); @@ -119,6 +121,8 @@ EXPORT int fwprintf_s(FILE *restrict stream, const wchar_t *restrict fmt, ...) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, -ret); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/fwscanf_s.c b/src/wchar/fwscanf_s.c index 45478fbd..60ec34ab 100644 --- a/src/wchar/fwscanf_s.c +++ b/src/wchar/fwscanf_s.c @@ -88,6 +88,7 @@ EXPORT int fwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, ...) { va_list ap; wchar_t *p; int ret; + int l_errno; if (unlikely(stream == NULL)) { invoke_safe_str_constraint_handler("fwscanf_s: stream is null", NULL, @@ -104,7 +105,6 @@ EXPORT int fwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, ...) { } #ifdef HAVE_MINGW32 if (unlikely(!*fmt)) { - errno = 0; return EOF; } #endif @@ -133,6 +133,7 @@ EXPORT int fwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, ...) { #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; va_start(ap, fmt); ret = vfwscanf(stream, fmt, ap); @@ -143,6 +144,8 @@ EXPORT int fwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, ...) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/mbsrtowcs_s.c b/src/wchar/mbsrtowcs_s.c index 7ec6e616..6fc53fb5 100644 --- a/src/wchar/mbsrtowcs_s.c +++ b/src/wchar/mbsrtowcs_s.c @@ -134,6 +134,7 @@ EXPORT errno_t _mbsrtowcs_s_chk(size_t *restrict retvalp, wchar_t *orig_dest; mbstate_t orig_ps; errno_t rc; + int l_errno; CHK_SRC_NULL("mbsrtowcs_s", retvalp) *retvalp = 0; @@ -184,6 +185,8 @@ EXPORT errno_t _mbsrtowcs_s_chk(size_t *restrict retvalp, orig_dest = dest; memcpy(&orig_ps, ps, sizeof(orig_ps)); + l_errno = errno; + errno = 0; *retvalp = mbsrtowcs(dest, srcp, len, ps); if (likely(*retvalp < dmax)) { @@ -198,7 +201,7 @@ EXPORT errno_t _mbsrtowcs_s_chk(size_t *restrict retvalp, } else { if (dest) { size_t tmp = 0; - errno = 0; + // errno = 0; /* with NULL either 0 or -1 is returned */ if (*retvalp > RSIZE_MAX_WSTR) { /* else ESNOSPC */ tmp = mbsrtowcs(NULL, srcp, len - 1, &orig_ps); @@ -214,6 +217,8 @@ EXPORT errno_t _mbsrtowcs_s_chk(size_t *restrict retvalp, rc = ((size_t)*retvalp == 0) ? EOK : errno; } } + if (0 == errno) + errno = l_errno; return RCNEGATE(rc); } diff --git a/src/wchar/mbstowcs_s.c b/src/wchar/mbstowcs_s.c index 7cc56b03..a1950f09 100644 --- a/src/wchar/mbstowcs_s.c +++ b/src/wchar/mbstowcs_s.c @@ -130,6 +130,7 @@ EXPORT errno_t _mbstowcs_s_chk(size_t *restrict retvalp, wchar_t *restrict dest, { wchar_t *orig_dest; errno_t rc; + int l_errno; #ifdef HAVE_CYGWIN64 mbstate_t st; #endif @@ -180,6 +181,7 @@ EXPORT errno_t _mbstowcs_s_chk(size_t *restrict retvalp, wchar_t *restrict dest, /* hold base of dest in case src was not copied */ orig_dest = dest; + l_errno = errno; errno = 0; *retvalp = mbstowcs(dest, src, len); @@ -196,7 +198,7 @@ EXPORT errno_t _mbstowcs_s_chk(size_t *restrict retvalp, wchar_t *restrict dest, } else { if (dest) { size_t tmp = 0; - errno = 0; + // errno = 0; if (*retvalp > RSIZE_MAX_WSTR) { /* else ESNOSPC */ tmp = mbstowcs(NULL, src, len); } @@ -212,6 +214,8 @@ EXPORT errno_t _mbstowcs_s_chk(size_t *restrict retvalp, wchar_t *restrict dest, rc = ((size_t)*retvalp == 0) ? EOK : errno; } } + if (0 == errno) + errno = l_errno; return RCNEGATE(rc); } diff --git a/src/wchar/snwprintf_s.c b/src/wchar/snwprintf_s.c index c5c21e39..a3350bfb 100644 --- a/src/wchar/snwprintf_s.c +++ b/src/wchar/snwprintf_s.c @@ -126,6 +126,7 @@ EXPORT int snwprintf_s(wchar_t *restrict dest, rsize_t dmax, va_list ap, ap2; wchar_t *p; int ret = -1; + int l_errno; const size_t destsz = dmax * sizeof(wchar_t); #if !(defined(SAFECLIB_HAVE_C99) && !defined(TEST_MSVCRT)) const size_t destbos = BOS_UNKNOWN; @@ -188,6 +189,7 @@ EXPORT int snwprintf_s(wchar_t *restrict dest, rsize_t dmax, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; /* C11 solves the ESNOSPC problem */ #ifdef HAVE_VSNWPRINTF_S @@ -242,6 +244,8 @@ EXPORT int snwprintf_s(wchar_t *restrict dest, rsize_t dmax, return ret; } #endif + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/swprintf_s.c b/src/wchar/swprintf_s.c index fde54177..36b57601 100644 --- a/src/wchar/swprintf_s.c +++ b/src/wchar/swprintf_s.c @@ -123,6 +123,7 @@ EXPORT int swprintf_s(wchar_t *restrict dest, rsize_t dmax, va_list ap, ap2; wchar_t *p; int ret = -1; + int l_errno; const size_t destsz = dmax * sizeof(wchar_t); #if !(defined(SAFECLIB_HAVE_C99) && !defined(TEST_MSVCRT)) const size_t destbos = BOS_UNKNOWN; @@ -189,6 +190,7 @@ EXPORT int swprintf_s(wchar_t *restrict dest, rsize_t dmax, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; va_start(ap, fmt); va_copy(ap2, ap); @@ -199,14 +201,15 @@ EXPORT int swprintf_s(wchar_t *restrict dest, rsize_t dmax, if (unlikely(ret == -1)) { if (unlikely(dmax == 1)) goto nospc; - errno = 0; if (likely(dmax < 512)) { /* stacksize 2k */ static wchar_t tmp[512]; + errno = 0; va_start(ap2, fmt); ret = vswprintf(tmp, 512, fmt, ap2); va_end(ap2); } else { wchar_t *tmp = (wchar_t *)malloc(dmax * sizeof(wchar_t)); + errno = 0; va_start(ap2, fmt); ret = vswprintf(tmp, dmax, fmt, ap2); va_end(ap2); @@ -214,7 +217,8 @@ EXPORT int swprintf_s(wchar_t *restrict dest, rsize_t dmax, } if (ret > 0) { nospc: - errno = 0; /* EOVERFLOW */ + if (0 == errno) + errno = l_errno; handle_werror(dest, dmax, "swprintf_s: len exceeds dmax", ESNOSPC); return -(ESNOSPC); } @@ -227,6 +231,8 @@ EXPORT int swprintf_s(wchar_t *restrict dest, rsize_t dmax, strcat(errstr, strerror(errno)); handle_werror(dest, dmax, errstr, -ret); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/swscanf_s.c b/src/wchar/swscanf_s.c index d8eb252d..cdf3af9b 100644 --- a/src/wchar/swscanf_s.c +++ b/src/wchar/swscanf_s.c @@ -90,6 +90,7 @@ EXPORT int swscanf_s(const wchar_t *restrict src, const wchar_t *restrict fmt, va_list ap; wchar_t *p; int ret; + int l_errno; if (unlikely(src == NULL)) { invoke_safe_str_constraint_handler("swscanf_s: src is null", NULL, @@ -134,6 +135,7 @@ EXPORT int swscanf_s(const wchar_t *restrict src, const wchar_t *restrict fmt, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; va_start(ap, fmt); ret = vswscanf(src, fmt, ap); @@ -144,6 +146,8 @@ EXPORT int swscanf_s(const wchar_t *restrict src, const wchar_t *restrict fmt, strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, (void *)src, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/vfwprintf_s.c b/src/wchar/vfwprintf_s.c index ba0447d2..36d36bd2 100644 --- a/src/wchar/vfwprintf_s.c +++ b/src/wchar/vfwprintf_s.c @@ -80,6 +80,7 @@ EXPORT int vfwprintf_s(FILE *restrict stream, const wchar_t *restrict fmt, va_list ap) { wchar_t *p; int ret; + int l_errno; if (unlikely(stream == NULL)) { invoke_safe_str_constraint_handler("vfwprintf_s: stream is null", NULL, @@ -115,6 +116,7 @@ EXPORT int vfwprintf_s(FILE *restrict stream, const wchar_t *restrict fmt, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; ret = vfwprintf(stream, fmt, ap); @@ -123,6 +125,8 @@ EXPORT int vfwprintf_s(FILE *restrict stream, const wchar_t *restrict fmt, strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, -ret); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/vfwscanf_s.c b/src/wchar/vfwscanf_s.c index d3e36122..8fde2666 100644 --- a/src/wchar/vfwscanf_s.c +++ b/src/wchar/vfwscanf_s.c @@ -92,6 +92,7 @@ EXPORT int vfwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, { wchar_t *p; int ret; + int l_errno; if (unlikely(stream == NULL)) { invoke_safe_str_constraint_handler("vfwscanf_s: stream is null", NULL, @@ -108,7 +109,7 @@ EXPORT int vfwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, } #ifdef HAVE_MINGW32 if (unlikely(!*fmt)) { - errno = 0; + // errno = 0; return EOF; } #endif @@ -137,6 +138,7 @@ EXPORT int vfwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; ret = vfwscanf(stream, fmt, ap); @@ -146,6 +148,8 @@ EXPORT int vfwscanf_s(FILE *restrict stream, const wchar_t *restrict fmt, invoke_safe_str_constraint_handler(errstr, NULL, errno); /* TODO: zero the out args */ } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/vsnwprintf_s.c b/src/wchar/vsnwprintf_s.c index 99e153b3..0d353fc3 100644 --- a/src/wchar/vsnwprintf_s.c +++ b/src/wchar/vsnwprintf_s.c @@ -127,6 +127,7 @@ EXPORT int _vsnwprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, #ifndef HAVE_VSNWPRINTF_S va_list ap2; #endif + int l_errno; if (unlikely(dest == NULL)) { invoke_safe_str_constraint_handler("vsnwprintf_s: dest is null", NULL, @@ -187,6 +188,7 @@ EXPORT int _vsnwprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; /* C11 solves the ESNOSPC problem */ #ifdef HAVE_VSNWPRINTF_S @@ -201,6 +203,8 @@ EXPORT int _vsnwprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, static wchar_t tmp[512]; if (unlikely(dmax == 1)) { *dest = L'\0'; + if (0 == errno) + errno = l_errno; return 1; } ret = vswprintf(tmp, 512, fmt, ap2); @@ -233,6 +237,8 @@ EXPORT int _vsnwprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, return ret; } #endif + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/vswprintf_s.c b/src/wchar/vswprintf_s.c index e1546239..e0fb25fc 100644 --- a/src/wchar/vswprintf_s.c +++ b/src/wchar/vswprintf_s.c @@ -112,6 +112,7 @@ EXPORT int _vswprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, { wchar_t *p; int ret = -1; + int l_errno; const size_t destsz = dmax * sizeof(wchar_t); #ifndef HAVE_VSNWPRINTF_S va_list ap2; @@ -171,6 +172,7 @@ EXPORT int _vswprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; /* C11 solves the ESNOSPC problem */ #ifdef HAVE_VSNWPRINTF_S @@ -195,7 +197,7 @@ EXPORT int _vswprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, } if (ret > 0) { nospc: - errno = 0; + // errno = 0; handle_werror(dest, dmax, "vswprintf_s: len exceeds dmax", ESNOSPC); return -(ESNOSPC); } @@ -212,6 +214,8 @@ EXPORT int _vswprintf_s_chk(wchar_t *restrict dest, rsize_t dmax, #endif *dest = 0; } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/vswscanf_s.c b/src/wchar/vswscanf_s.c index 8128f6ec..4e2d4c84 100644 --- a/src/wchar/vswscanf_s.c +++ b/src/wchar/vswscanf_s.c @@ -88,6 +88,7 @@ EXPORT int vswscanf_s(const wchar_t *restrict src, const wchar_t *restrict fmt, va_list ap) { wchar_t *p; int ret; + int l_errno; if (unlikely(src == NULL)) { invoke_safe_str_constraint_handler("vswscanf_s: src is null", NULL, @@ -132,6 +133,7 @@ EXPORT int vswscanf_s(const wchar_t *restrict src, const wchar_t *restrict fmt, #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; ret = vswscanf(src, fmt, ap); @@ -140,6 +142,8 @@ EXPORT int vswscanf_s(const wchar_t *restrict src, const wchar_t *restrict fmt, strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, (void *)src, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/vwprintf_s.c b/src/wchar/vwprintf_s.c index 54362cf6..ddd824f8 100644 --- a/src/wchar/vwprintf_s.c +++ b/src/wchar/vwprintf_s.c @@ -75,6 +75,7 @@ any of the arguments corresponding to %s is a null pointer EXPORT int vwprintf_s(const wchar_t *restrict fmt, va_list ap) { wchar_t *p; int ret; + int l_errno; if (unlikely(fmt == NULL)) { invoke_safe_str_constraint_handler("vwprintf_s: fmt is null", NULL, @@ -104,6 +105,7 @@ EXPORT int vwprintf_s(const wchar_t *restrict fmt, va_list ap) { #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; ret = vwprintf(fmt, ap); @@ -112,6 +114,8 @@ EXPORT int vwprintf_s(const wchar_t *restrict fmt, va_list ap) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, -ret); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/vwscanf_s.c b/src/wchar/vwscanf_s.c index 57d502ff..3646ea7f 100644 --- a/src/wchar/vwscanf_s.c +++ b/src/wchar/vwscanf_s.c @@ -87,6 +87,7 @@ EXPORT int vwscanf_s(const wchar_t *restrict fmt, va_list ap) { wchar_t *p; int ret; + int l_errno; if (unlikely(fmt == NULL)) { invoke_safe_str_constraint_handler("vwscanf_s: fmt is null", NULL, @@ -124,6 +125,7 @@ EXPORT int vwscanf_s(const wchar_t *restrict fmt, va_list ap) #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; ret = vwscanf(fmt, ap); @@ -132,6 +134,8 @@ EXPORT int vwscanf_s(const wchar_t *restrict fmt, va_list ap) strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; } diff --git a/src/wchar/wcstok_s.c b/src/wchar/wcstok_s.c index e26d6611..a17335c4 100644 --- a/src/wchar/wcstok_s.c +++ b/src/wchar/wcstok_s.c @@ -189,6 +189,7 @@ EXPORT wchar_t *_wcstok_s_chk(wchar_t *restrict dest, rsize_t *restrict dmaxp, rsize_t slen; rsize_t destsz; const wchar_t *orig_dest = dest; + int l_errno; if (unlikely(dmaxp == NULL)) { invoke_safe_str_constraint_handler("wcstok_s: dmaxp is NULL", @@ -264,6 +265,7 @@ EXPORT wchar_t *_wcstok_s_chk(wchar_t *restrict dest, rsize_t *restrict dmaxp, * scan dest for a delimiter */ ptoken = NULL; + l_errno = errno; errno = 0; while (*dest != L'\0' && !ptoken) { @@ -314,6 +316,8 @@ EXPORT wchar_t *_wcstok_s_chk(wchar_t *restrict dest, rsize_t *restrict dmaxp, */ if (ptoken == NULL) { *dmaxp = dlen; + if (0 == errno) + errno = l_errno; return (ptoken); } @@ -367,6 +371,8 @@ EXPORT wchar_t *_wcstok_s_chk(wchar_t *restrict dest, rsize_t *restrict dmaxp, dlen--; } + if (0 == errno) + errno = l_errno; *dmaxp = dlen; return (ptoken); } diff --git a/src/wchar/wscanf_s.c b/src/wchar/wscanf_s.c index 7ab446dc..0df9a26a 100644 --- a/src/wchar/wscanf_s.c +++ b/src/wchar/wscanf_s.c @@ -89,6 +89,7 @@ EXPORT int wscanf_s(const wchar_t *restrict fmt, ...) { va_list ap; wchar_t *p; int ret; + int l_errno; if (unlikely(fmt == NULL)) { invoke_safe_str_constraint_handler("wscanf_s: fmt is null", NULL, @@ -121,6 +122,7 @@ EXPORT int wscanf_s(const wchar_t *restrict fmt, ...) { #error need wcsstr or wcschr #endif + l_errno = errno; errno = 0; va_start(ap, fmt); ret = vwscanf(fmt, ap); @@ -131,6 +133,8 @@ EXPORT int wscanf_s(const wchar_t *restrict fmt, ...) { strcat(errstr, strerror(errno)); invoke_safe_str_constraint_handler(errstr, NULL, errno); } + if (0 == errno) + errno = l_errno; return ret; }