Skip to content

Commit b088e01

Browse files
committed
restore errno if set
Fixes GH #129
1 parent c752e8f commit b088e01

34 files changed

+151
-10
lines changed

src/io/fopen_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ EXPORT errno_t fopen_s(FILE *restrict *restrict streamptr,
7979
const char *restrict filename,
8080
const char *restrict mode) {
8181

82+
int l_errno;
8283
if (unlikely(streamptr == NULL)) {
8384
invoke_safe_str_constraint_handler("fopen_s: streamptr is null", NULL,
8485
ESNULLP);
@@ -97,6 +98,7 @@ EXPORT errno_t fopen_s(FILE *restrict *restrict streamptr,
9798
return ESNULLP;
9899
}
99100

101+
l_errno = errno;
100102
errno = 0;
101103
*streamptr = fopen(filename, mode);
102104

@@ -106,6 +108,8 @@ EXPORT errno_t fopen_s(FILE *restrict *restrict streamptr,
106108
invoke_safe_str_constraint_handler(errstr, NULL, errno);
107109
return errno;
108110
}
111+
if (0 == errno)
112+
errno = l_errno;
109113

110114
return EOK;
111115
}

src/io/fprintf_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ EXPORT int fprintf_s(FILE *restrict stream, const char *restrict fmt, ...) {
7272
int ret;
7373
const char *p;
7474
out_fct_wrap_type wrap;
75+
int l_errno;
7576

7677
if (unlikely(stream == NULL)) {
7778
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, ...) {
9293
}
9394
}
9495

96+
l_errno = errno;
9597
errno = 0;
9698
wrap.arg = stream;
9799
va_start(ap, fmt);
@@ -103,6 +105,8 @@ EXPORT int fprintf_s(FILE *restrict stream, const char *restrict fmt, ...) {
103105
strcat(errstr, strerror(errno));
104106
invoke_safe_str_constraint_handler(errstr, NULL, -ret);
105107
}
108+
if (0 == errno)
109+
errno = l_errno;
106110

107111
return ret;
108112
}

src/io/freopen_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ EXPORT errno_t freopen_s(FILE *restrict *restrict newstreamptr,
8383
const char *restrict filename,
8484
const char *restrict mode, FILE *restrict stream) {
8585

86+
int l_errno;
8687
if (unlikely(newstreamptr == NULL)) {
8788
invoke_safe_str_constraint_handler("freopen_s: newstreamptr is null",
8889
NULL, ESNULLP);
@@ -101,6 +102,7 @@ EXPORT errno_t freopen_s(FILE *restrict *restrict newstreamptr,
101102
return ESNULLP;
102103
}
103104

105+
l_errno = errno;
104106
errno = 0;
105107
*newstreamptr = freopen(filename, mode, stream);
106108

@@ -110,6 +112,8 @@ EXPORT errno_t freopen_s(FILE *restrict *restrict newstreamptr,
110112
invoke_safe_str_constraint_handler(errstr, NULL, errno);
111113
return errno;
112114
}
115+
if (0 == errno)
116+
errno = l_errno;
113117

114118
return EOK;
115119
}

src/io/fscanf_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ any of the arguments corresponding to %s is a null pointer.
8686
EXPORT int fscanf_s(FILE *restrict stream, const char *restrict fmt, ...) {
8787
va_list ap;
8888
int ret;
89+
int l_errno;
8990
#if defined(HAVE_STRSTR)
9091
char *p;
9192
#endif
@@ -126,6 +127,7 @@ EXPORT int fscanf_s(FILE *restrict stream, const char *restrict fmt, ...) {
126127
}
127128
#endif
128129

130+
l_errno = errno;
129131
errno = 0;
130132
va_start(ap, fmt);
131133
ret = vfscanf(stream, fmt, ap);
@@ -136,6 +138,8 @@ EXPORT int fscanf_s(FILE *restrict stream, const char *restrict fmt, ...) {
136138
strcat(errstr, strerror(errno));
137139
invoke_safe_str_constraint_handler(errstr, NULL, errno);
138140
}
141+
if (0 == errno)
142+
errno = l_errno;
139143

140144
return ret;
141145
}

src/io/gets_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ EXPORT char *_gets_s_chk(char *restrict dest, rsize_t dmax,
109109
#endif
110110
{
111111
char *ret;
112+
int l_errno;
112113

113114
if (unlikely(dest == NULL)) {
114115
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,
145146
}
146147
}
147148

149+
l_errno = errno;
148150
errno = 0;
149151
ret = fgets(dest, dmax + 1, stdin);
150152

@@ -171,6 +173,8 @@ EXPORT char *_gets_s_chk(char *restrict dest, rsize_t dmax,
171173
#endif
172174
}
173175
}
176+
if (0 == errno)
177+
errno = l_errno;
174178

175179
return ret;
176180
}

src/io/scanf_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ any of the arguments corresponding to %s is a null pointer.
8787
EXPORT int scanf_s(const char *restrict fmt, ...) {
8888
va_list ap;
8989
int ret;
90+
int l_errno;
9091
#if defined(HAVE_STRSTR)
9192
char *p;
9293
#endif
@@ -120,6 +121,7 @@ EXPORT int scanf_s(const char *restrict fmt, ...) {
120121
}
121122
#endif
122123

124+
l_errno = errno;
123125
errno = 0;
124126
va_start(ap, fmt);
125127
ret = vscanf(fmt, ap);
@@ -130,6 +132,8 @@ EXPORT int scanf_s(const char *restrict fmt, ...) {
130132
strcat(errstr, strerror(errno));
131133
invoke_safe_str_constraint_handler(errstr, NULL, errno);
132134
}
135+
if (0 == errno)
136+
errno = l_errno;
133137

134138
return ret;
135139
}

src/io/sscanf_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ EXPORT int sscanf_s(const char *restrict buffer, const char *restrict fmt,
8989
...) {
9090
va_list ap;
9191
int ret;
92+
int l_errno;
9293
#if defined(HAVE_STRSTR)
9394
char *p;
9495
#endif
@@ -129,6 +130,7 @@ EXPORT int sscanf_s(const char *restrict buffer, const char *restrict fmt,
129130
}
130131
#endif
131132

133+
l_errno = errno;
132134
errno = 0;
133135
va_start(ap, fmt);
134136
ret = vsscanf(buffer, fmt, ap);
@@ -139,6 +141,8 @@ EXPORT int sscanf_s(const char *restrict buffer, const char *restrict fmt,
139141
strcat(errstr, strerror(errno));
140142
invoke_safe_str_constraint_handler(errstr, NULL, errno);
141143
}
144+
if (0 == errno)
145+
errno = l_errno;
142146

143147
return ret;
144148
}

src/io/tmpfile_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080

8181
EXPORT errno_t tmpfile_s(FILE *restrict *restrict streamptr) {
8282
static int count = 0;
83+
int l_errno;
8384

8485
if (unlikely(streamptr == NULL)) {
8586
invoke_safe_str_constraint_handler("tmpfile_s: streamptr is null", NULL,
@@ -95,6 +96,7 @@ EXPORT errno_t tmpfile_s(FILE *restrict *restrict streamptr) {
9596
return ESLEMAX;
9697
}
9798

99+
l_errno = errno;
98100
errno = 0;
99101
*streamptr = tmpfile();
100102

@@ -104,6 +106,8 @@ EXPORT errno_t tmpfile_s(FILE *restrict *restrict streamptr) {
104106
invoke_safe_str_constraint_handler(errstr, NULL, errno);
105107
return errno;
106108
}
109+
if (0 == errno)
110+
errno = l_errno;
107111

108112
return EOK;
109113
}

src/io/vfprintf_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
EXPORT int vfprintf_s(FILE *restrict stream, const char *restrict fmt,
7272
va_list ap) {
7373
int ret;
74+
int l_errno;
7475
const char *p;
7576
out_fct_wrap_type wrap;
7677

@@ -100,6 +101,7 @@ EXPORT int vfprintf_s(FILE *restrict stream, const char *restrict fmt,
100101
}
101102
}
102103

104+
l_errno = errno;
103105
errno = 0;
104106
#if 0
105107
ret = vfprintf(stream, fmt, ap);
@@ -118,6 +120,8 @@ EXPORT int vfprintf_s(FILE *restrict stream, const char *restrict fmt,
118120
invoke_safe_str_constraint_handler(errstr, NULL, -ret);
119121
}
120122
#endif
123+
if (0 == errno)
124+
errno = l_errno;
121125

122126
return ret;
123127
}

src/io/vfscanf_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ EXPORT int vfscanf_s(FILE *restrict stream, const char *restrict fmt,
8989
char *p;
9090
#endif
9191
int ret;
92+
int l_errno;
9293

9394
if (unlikely(stream == NULL)) {
9495
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,
126127
}
127128
#endif
128129

130+
l_errno = errno;
129131
errno = 0;
130132
ret = vfscanf(stream, fmt, ap);
131133

@@ -134,6 +136,8 @@ EXPORT int vfscanf_s(FILE *restrict stream, const char *restrict fmt,
134136
strcat(errstr, strerror(errno));
135137
invoke_safe_str_constraint_handler(errstr, NULL, errno);
136138
}
139+
if (0 == errno)
140+
errno = l_errno;
137141

138142
return ret;
139143
}

0 commit comments

Comments
 (0)