Skip to content

Commit

Permalink
restore errno if set
Browse files Browse the repository at this point in the history
Fixes GH #129
  • Loading branch information
rurban committed Mar 8, 2024
1 parent 13c8fef commit dbbb78d
Show file tree
Hide file tree
Showing 34 changed files with 151 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/io/fopen_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -97,6 +98,7 @@ EXPORT errno_t fopen_s(FILE *restrict *restrict streamptr,
return ESNULLP;
}

l_errno = errno;
errno = 0;
*streamptr = fopen(filename, mode);

Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/io/fprintf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/io/freopen_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/io/fscanf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/io/gets_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -171,6 +173,8 @@ EXPORT char *_gets_s_chk(char *restrict dest, rsize_t dmax,
#endif
}
}
if (0 == errno)
errno = l_errno;

return ret;
}
4 changes: 4 additions & 0 deletions src/io/scanf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/io/sscanf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/io/tmpfile_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -95,6 +96,7 @@ EXPORT errno_t tmpfile_s(FILE *restrict *restrict streamptr) {
return ESLEMAX;
}

l_errno = errno;
errno = 0;
*streamptr = tmpfile();

Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/io/vfprintf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -98,6 +99,7 @@ EXPORT int vfprintf_s(FILE *restrict stream, const char *restrict fmt,
}
}

l_errno = errno;
errno = 0;
#if 0
ret = vfprintf(stream, fmt, ap);
Expand All @@ -116,6 +118,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;
}
4 changes: 4 additions & 0 deletions src/io/vfscanf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/io/vprintf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -84,6 +85,7 @@ EXPORT int vprintf_s(const char *restrict fmt, va_list ap) {
}
}

l_errno = errno;
errno = 0;
ret = vprintf(fmt, ap);

Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/io/vscanf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/io/vsscanf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/misc/bsearch_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/os/getenv_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Loading

0 comments on commit dbbb78d

Please sign in to comment.