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 <stdint.h> 28 #include <stdlib.h> 29 #include <string.h> 30 31 static errno_t e; 32 static const char *_RESTRICT_KYWD m; 33 34 void 35 h(const char *_RESTRICT_KYWD msg, void *_RESTRICT_KYWD ptr, errno_t error) 36 { 37 e = error; 38 m = msg; 39 } 40 41 int 42 main(void) 43 { 44 char a; 45 char b[3]; 46 47 /* null ptr */ 48 set_constraint_handler_s(ignore_handler_s); 49 assert(memset_s(0, 1, 1, 1) != 0); 50 51 /* smax > rmax */ 52 set_constraint_handler_s(ignore_handler_s); 53 assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0); 54 55 /* smax < 0 */ 56 set_constraint_handler_s(ignore_handler_s); 57 assert(memset_s(&a, -1, 1, 1) != 0); 58 59 /* normal */ 60 set_constraint_handler_s(ignore_handler_s); 61 a = 3; 62 assert(memset_s(&a, 1, 5, 1) == 0); 63 assert(a == 5); 64 65 /* n > rmax */ 66 set_constraint_handler_s(ignore_handler_s); 67 assert(memset_s(&a, 1, 1, RSIZE_MAX + 1) != 0); 68 69 /* n < 0 */ 70 set_constraint_handler_s(ignore_handler_s); 71 assert(memset_s(&a, 1, 1, -1) != 0); 72 73 /* n < smax */ 74 set_constraint_handler_s(ignore_handler_s); 75 b[0] = 1; b[1] = 2; b[2] = 3; 76 assert(memset_s(&b[0], 3, 9, 1) == 0); 77 assert(b[0] == 9); 78 assert(b[1] == 2); 79 assert(b[2] == 3); 80 81 /* n > smax, handler */ 82 set_constraint_handler_s(h); 83 e = 0; 84 m = NULL; 85 b[0] = 1; b[1] = 2; b[2] = 3; 86 assert(memset_s(&b[0], 1, 9, 3) != 0); 87 assert(e > 0); 88 assert(strcmp(m, "memset_s: n > smax") == 0); 89 assert(b[0] == 9); 90 assert(b[1] == 2); 91 assert(b[2] == 3); 92 93 /* smax > rmax, handler */ 94 set_constraint_handler_s(h); 95 e = 0; 96 m = NULL; 97 assert(memset_s(&a, RSIZE_MAX + 1, 1, 1) != 0); 98 assert(e > 0); 99 assert(strcmp(m, "memset_s: smax > RSIZE_MAX") == 0); 100 101 /* smax < 0, handler */ 102 set_constraint_handler_s(h); 103 e = 0; 104 m = NULL; 105 assert(memset_s(&a, -1, 1, 1) != 0); 106 assert(e > 0); 107 assert(strcmp(m, "memset_s: smax > RSIZE_MAX") == 0); 108 109 /* n > rmax, handler */ 110 set_constraint_handler_s(h); 111 e = 0; 112 m = NULL; 113 assert(memset_s(&a, 1, 1, RSIZE_MAX + 1) != 0); 114 assert(e > 0); 115 assert(strcmp(m, "memset_s: n > RSIZE_MAX") == 0); 116 117 /* n < 0, handler */ 118 set_constraint_handler_s(h); 119 e = 0; 120 m = NULL; 121 assert(memset_s(&a, 1, 1, -1) != 0); 122 assert(e > 0); 123 assert(strcmp(m, "memset_s: n > RSIZE_MAX") == 0); 124 125 return (0); 126 }