File: test_memchr_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 (143 lines) | stat: -rw-r--r-- 3,140 bytes parent folder | download
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
/*------------------------------------------------------------------
 * test_memchr_s
 * File 'memchr_s.c'
 * Lines executed:94.12% of 17
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_mem_lib.h"

#define LEN (128)

static uint8_t mem[LEN];
int test_memchr_s(void);

int test_memchr_s(void) {
    errno_t rc;
#ifndef HAVE_CT_BOS_OVR
    int ch;
#endif
    void *sub;
    void *std_sub;

    size_t len;
    int errs = 0;

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

#ifndef HAVE_CT_BOS_OVR
    ch = 0;
    EXPECT_BOS("empty dest")
    rc = memchr_s(NULL, LEN, ch, &sub);
    ERR(ESNULLP);
    SUBNULL();

    EXPECT_BOS("empty resultp")
    rc = memchr_s(mem, LEN, ch, NULL);
    ERR(ESNULLP);

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

    EXPECT_BOS("empty dest or dmax")
    rc = memchr_s(mem, 0, ch, &sub);
    ERR(ESZEROL)
    SUBNULL();

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

    EXPECT_BOS("dest overflow")
    rc = memchr_s(mem, RSIZE_MAX_MEM + 1, ch, &sub);
    ERR(ESLEMAX)
    SUBNULL();

#ifdef HAVE___BUILTIN_OBJECT_SIZE
    EXPECT_BOS("dest overflow")
    rc = memchr_s(mem, LEN + 1, ch, &sub);
    ERR(EOVERFLOW)
    SUBNULL();
#endif

    EXPECT_BOS("ch overflow >255")
    rc = memchr_s(mem, LEN, 256, &sub);
    ERR(ESLEMAX)
    SUBNULL();
#endif

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

    *mem = '\0';
    rc = memchr_s(mem, LEN, 0, &sub);
    ERR(EOK);
    PTREQ(sub, mem);

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

    len = 20;
    /* 012345678901234567890 */
    memcpy(mem, "keep it all together", len);

    rc = memchr_s(mem, LEN, 0, &sub);
    ERR(EOK);
    PTREQ(sub, &mem[len]);

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

    /* at beginning */
    rc = memchr_s(mem, LEN, 'k', &sub);
    ERR(EOK)
    PTREQ(sub, &mem[0]);

    /* in the middle - left */
    rc = memchr_s(mem, LEN, 'e', &sub);
    ERR(EOK)
    PTREQ(sub, &mem[1]);

    /* in the middle - right */
    rc = memchr_s(mem, LEN, 'o', &sub);
    ERR(EOK)
    PTREQ(sub, &mem[13]);

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

    rc = memchr_s(mem, len, 'h', &sub);
    ERR(EOK)
    PTREQ(sub, &mem[17]);

    rc = memchr_s(mem, len, 'r', &sub);
    ERR(EOK)
    PTREQ(sub, &mem[19]);

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

    rc = memchr_s(mem, 3, 'i', &sub);
    ERR(ESNOTFND)
    SUBNULL();

    rc = memchr_s(mem, LEN, 'i', &sub);
    ERR(EOK)

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

    rc = memchr_s(mem, len, 1, &sub);
    ERR(ESNOTFND);
    SUBNULL();

    rc = memchr_s(mem, LEN, 'e', &sub);
    ERR(EOK)
    PTREQ(sub, &mem[1]);
    /* compare to legacy */
    std_sub = memchr(mem, 'e', LEN);
    PTREQ(sub, std_sub);

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

    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_memchr_s()); }
#endif