1 /*
2 * Copyright (c) 2017 Juniper Networks. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 static errno_t e;
33 static const char *_RESTRICT_KYWD m;
34
35 void
h(const char * _RESTRICT_KYWD msg,void * _RESTRICT_KYWD ptr,errno_t error)36 h(const char *_RESTRICT_KYWD msg, void *_RESTRICT_KYWD ptr, errno_t error)
37 {
38 e = error;
39 m = msg;
40 }
41
42 int
main(void)43 main(void)
44 {
45 char a;
46 char b[3];
47
48 /* null ptr */
49 (void) set_constraint_handler_s(ignore_handler_s);
50 assert(memset_s(0, 1, 1, 1) != 0);
51
52 /* smax > rmax */
53 (void) set_constraint_handler_s(ignore_handler_s);
54 assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0);
55
56 /* smax < 0 */
57 (void) set_constraint_handler_s(ignore_handler_s);
58 assert(memset_s(&a, -1, 1, 1) != 0);
59
60 /* normal */
61 (void) set_constraint_handler_s(ignore_handler_s);
62 a = 3;
63 assert(memset_s(&a, 1, 5, 1) == 0);
64 assert(a == 5);
65
66 /* n > rmax */
67 (void) set_constraint_handler_s(ignore_handler_s);
68 assert(memset_s(&a, 1, 1, RSIZE_MAX + 1) != 0);
69
70 /* n < 0 */
71 (void) set_constraint_handler_s(ignore_handler_s);
72 assert(memset_s(&a, 1, 1, -1) != 0);
73
74 /* n < smax */
75 (void) set_constraint_handler_s(ignore_handler_s);
76 b[0] = 1; b[1] = 2; b[2] = 3;
77 assert(memset_s(&b[0], 3, 9, 1) == 0);
78 assert(b[0] == 9);
79 assert(b[1] == 2);
80 assert(b[2] == 3);
81
82 /* n > smax, handler */
83 (void) set_constraint_handler_s(h);
84 e = 0;
85 m = NULL;
86 b[0] = 1; b[1] = 2; b[2] = 3;
87 assert(memset_s(&b[0], 1, 9, 3) != 0);
88 assert(e > 0);
89 assert(strcmp(m, "memset_s: n > smax") == 0);
90 assert(b[0] == 9);
91 assert(b[1] == 2);
92 assert(b[2] == 3);
93
94 /* smax > rmax, handler */
95 (void) set_constraint_handler_s(h);
96 e = 0;
97 m = NULL;
98 assert(memset_s(&a, RSIZE_MAX + 1, 1, 1) != 0);
99 assert(e > 0);
100 assert(strcmp(m, "memset_s: smax > RSIZE_MAX") == 0);
101
102 /* smax < 0, handler */
103 (void) set_constraint_handler_s(h);
104 e = 0;
105 m = NULL;
106 assert(memset_s(&a, -1, 1, 1) != 0);
107 assert(e > 0);
108 assert(strcmp(m, "memset_s: smax > RSIZE_MAX") == 0);
109
110 /* n > rmax, handler */
111 (void) set_constraint_handler_s(h);
112 e = 0;
113 m = NULL;
114 assert(memset_s(&a, 1, 1, RSIZE_MAX + 1) != 0);
115 assert(e > 0);
116 assert(strcmp(m, "memset_s: n > RSIZE_MAX") == 0);
117
118 /* n < 0, handler */
119 (void) set_constraint_handler_s(h);
120 e = 0;
121 m = NULL;
122 assert(memset_s(&a, 1, 1, -1) != 0);
123 assert(e > 0);
124 assert(strcmp(m, "memset_s: n > RSIZE_MAX") == 0);
125
126 return (0);
127 }
128