File: test_memset_s.c

package info (click to toggle)
safeclib 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: ansic: 52,639; makefile: 1,271; perl: 528; sh: 518
file content (206 lines) | stat: -rw-r--r-- 4,868 bytes parent folder | download | duplicates (3)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*------------------------------------------------------------------
 * test_memset_s
 * File 'memset_s.c'
 * Lines executed:94.44% of 18
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "test_expmem.h"
#if defined(TEST_MSVCRT) && defined(HAVE_MEMSET_S)
#undef memset_s
#endif

#ifdef HAVE_MEMSET_S
#define HAVE_NATIVE 1
#else
#define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"

#define LEN (256)
#define MAX RSIZE_MAX_MEM

static uint8_t mem1[LEN];
int test_memset_s(void);

int test_memset_s(void) {
    errno_t rc;
    uint32_t len;
    uint32_t i;

    uint8_t value;
    int errs = 0;

    /*--------------------------------------------------*/

#if defined HAVE_MEMSET_S
#if defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ >= 1)
    /* no compile-time checks for memset_s, since it's overridden from libc */
#undef EXPECT_BOS
#define EXPECT_BOS(s)
#endif
    use_msvcrt = true;
#else
    use_msvcrt = false;
#endif
    value = 34;
#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty dest")
    rc = memset_s(NULL, LEN, value, 0);
    if (rc == ESNULLP) {
        if (use_msvcrt)
            printf("safec overriding libc\n");
        use_msvcrt = false;
    } else {
        if (!use_msvcrt)
            printf("libc overriding safec\n");
        use_msvcrt = true;
    }
    ERR_MSVC(ESNULLP, EINVAL);

    for (i = 0; i < LEN; i++) {
        mem1[i] = 33;
    }
    value = 34;

    /* first check dest, then n */
    EXPECT_BOS("empty dest")
    rc = memset_s(NULL, LEN, value, 0);
    ERR_MSVC(ESNULLP, EINVAL);

    EXPECT_BOS("value overflow >255")
    rc = memset_s(mem1, LEN, 256,
                  LEN);   /* This should be caught at compile-time */
    ERR_MSVC(ESLEMAX, 0); /* no native overflow check on darwin! */
    if (use_msvcrt)
        EXPMEM(mem1, 0, LEN, 0, 1);

    for (i = 0; i < LEN; i++) {
        mem1[i] = 33;
    }
    EXPECT_BOS("dest overflow or empty")
    rc = memset_s(mem1, MAX + 1, value, LEN);
    ERR_MSVC(ESLEMAX, 0); /* and implementation defined */
    if (use_msvcrt)
        EXPMEM(mem1, 0, LEN, value, 1);

    for (i = 0; i < LEN; i++) {
        mem1[i] = 33;
    }
    EXPECT_BOS("n overflow >dest/dmax")
    rc = memset_s(mem1, LEN, value, MAX + 1);
    ERR_MSVC(ESLEMAX, EOVERFLOW); /* and set all */
    EXPMEM(mem1, 0, LEN, value, 1);

#ifdef HAVE___BUILTIN_OBJECT_SIZE
    EXPECT_BOS("n overflow >dest/dmax")
    rc = memset_s(mem1, LEN, value, LEN + 1);
    ERR_MSVC(ESNOSPC, EOVERFLOW); /* and set all */
    EXPMEM(mem1, 0, LEN, value, 1);
#endif
#endif

    /*--------------------------------------------------*/

    for (i = 0; i < LEN; i++) {
        mem1[i] = 33;
    }
    value = 34;

    /* check n first, then args 2-3 */
    rc = memset_s(mem1, LEN, value, 0);
    ERR(EOK); /* and untouched */
    EXPMEM(mem1, 0, LEN, 33, 1);

    rc = memset_s(mem1, 0, value, 0);
    ERR(EOK); /* still untouched */
    EXPMEM(mem1, 0, LEN, 33, 1);

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("value overflow >255")
    rc = memset_s(mem1, LEN, 256, 0); /* should error! */
    ERR(EOK);                         /* still untouched */
    EXPMEM(mem1, 0, LEN, 33, 1);
#endif
    /*--------------------------------------------------*/

    for (i = 0; i < LEN; i++) {
        mem1[i] = 99;
    }

    len = 1;
    value = 34;

    rc = memset_s(mem1, LEN, value, len);
    ERR(EOK);
    EXPMEM(mem1, 0, len, value, 1);
    EXPMEM(mem1, len, LEN, 99, 1);

    /*--------------------------------------------------*/

    for (i = 0; i < LEN; i++) {
        mem1[i] = 99;
    }

    len = 2;
    value = 34;

    rc = memset_s(mem1, LEN, value, len);
    ERR(EOK);
    EXPMEM(mem1, 0, len, value, 1);
    EXPMEM(mem1, len, LEN, 99, 1);

    /*--------------------------------------------------*/

    for (i = 0; i < LEN; i++) {
        mem1[i] = 99;
    }

    len = 12;
    value = 34;

    rc = memset_s(mem1, LEN, value, len);
    ERR(EOK);
    EXPMEM(mem1, 0, len, value, 1);
    EXPMEM(mem1, len, LEN, 99, 1);

    /*--------------------------------------------------*/

    for (i = 0; i < LEN; i++) {
        mem1[i] = 99;
    }

    len = 31;
    value = 34;

    rc = memset_s(mem1, LEN, value, len);
    ERR(EOK);
    EXPMEM(mem1, 0, len, value, 1);
    EXPMEM(mem1, len, LEN, 99, 1);

    /*--------------------------------------------------*/

    for (i = 0; i < LEN; i++) {
        mem1[i] = 99;
    }

    len = 133;
    value = 34;

    rc = memset_s(mem1, LEN, value, len);
    ERR(EOK);
    EXPMEM(mem1, 0, len, value, 1);
    EXPMEM(mem1, len, LEN, 99, 1);

    /*--------------------------------------------------*/

    return (errs);
}

#ifndef __KERNEL__
/* simple hack to get this to work for both userspace and Linux kernel,
   until a better solution can be created. */
int main(void) { return (test_memset_s()); }
#endif