forked from rurban/safeclib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_asctime_s.c
141 lines (117 loc) · 4.08 KB
/
test_asctime_s.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*------------------------------------------------------------------
* test_asctime_s
* File 'os/asctime_s.c'
* Lines executed:77.08% of 48
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "safe_lib.h"
#ifdef HAVE_ASCTIME_S
#define HAVE_NATIVE 1
#else
#define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"
#define LEN (128)
static char str1[LEN];
int test_asctime_s(void);
int test_asctime_s(void) {
errno_t rc;
int errs = 0;
int old;
int ind;
int len;
struct tm *tm;
const time_t timet = 20000000;
char *str2;
tm = gmtime(&timet);
/*--------------------------------------------------*/
/* even static we might use the native forceinline asctime_s */
#if defined(_WIN32) && (HAVE_NATIVE)
use_msvcrt = true;
#endif
print_msvcrt(use_msvcrt);
#ifndef HAVE_CT_BOS_OVR
EXPECT_BOS("empty dest")
rc = asctime_s(NULL, LEN, tm);
init_msvcrt(rc == ESNULLP, &use_msvcrt);
ERR_MSVC(ESNULLP, EINVAL);
EXPECT_BOS("empty tm")
rc = asctime_s(str1, LEN, NULL);
ERR_MSVC(ESNULLP, EINVAL);
/*--------------------------------------------------*/
EXPECT_BOS("empty dest or dmax") EXPECT_BOS("dmax underflow")
rc = asctime_s(str1, 0, tm);
ERR_MSVC(ESLEMIN, EINVAL);
EXPECT_BOS("dmax underflow")
rc = asctime_s(str1, 25, tm);
ERR_MSVC(ESLEMIN, EINVAL);
EXPECT_BOS("dest overflow")
rc = asctime_s(str1, RSIZE_MAX_STR + 1, tm);
ERR_MSVC(ESLEMAX, 0);
#endif
/*--------------------------------------------------*/
#define TM_RANGE(memb, mmin, mmax) \
old = tm->tm_##memb; \
tm->tm_##memb = mmin - 1; \
rc = asctime_s(str1, LEN, tm); \
ERR_MSVC(ESLEMIN, EINVAL); \
\
tm->tm_##memb = mmax + 1; \
rc = asctime_s(str1, LEN, tm); \
ERR_MSVC(ESLEMAX, EINVAL); \
\
tm->tm_##memb = mmin; \
rc = asctime_s(str1, LEN, tm); \
ERR(EOK); \
tm->tm_##memb = mmax; \
rc = asctime_s(str1, LEN, tm); \
tm->tm_##memb = old; \
ERR(EOK)
#ifndef HAVE_MINGW32
TM_RANGE(sec, 0, 60);
#endif
TM_RANGE(min, 0, 59);
TM_RANGE(hour, 0, 23);
if (use_msvcrt) {
old = tm->tm_mday;
tm->tm_mday = 0;
rc = asctime_s(str1, LEN, tm);
tm->tm_mday = old;
}
TM_RANGE(mday, 1, 31);
TM_RANGE(mon, 0, 11);
if (!use_msvcrt) {
TM_RANGE(year, 0, 8099);
TM_RANGE(wday, 0, 6);
TM_RANGE(yday, 0, 365);
TM_RANGE(isdst, 0, 1);
}
/* stack buffer branch */
tm = gmtime(&timet);
tm->tm_year = 0;
rc = asctime_s(str1, 40, tm);
ERR(EOK);
len = strlen(str1);
CHECK_SLACK(&str1[len], 40 - len);
/* darwin asctime_r overflows >= 8100 */
for (ind = 8090; ind <= 8200; ind++) {
tm->tm_year = ind;
rc = asctime_s(str1, 40, tm);
if (rc == -1) {
ERR(EOK);
debug_printf("year %d => %d ", ind, rc);
}
}
/*--------------------------------------------------*/
tm = gmtime(&timet);
rc = asctime_s(str1, 40, tm);
str2 = asctime(tm);
EXPSTR(str1, str2);
len = strlen(str1);
CHECK_SLACK(&str1[len], 40 - len);
/*--------------------------------------------------*/
return (errs);
}
int main(void) { return (test_asctime_s()); }