1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2024, Synopsys Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #define __STDC_WANT_LIB_EXT1__ 1
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 
42 #define MAX_ERROR_MSG 100
43 
44 char handler_msg[MAX_ERROR_MSG] = "";
45 
46 static void
custom_constraint_handler(const char * restrict msg,void * restrict ptr,errno_t error)47 custom_constraint_handler(const char *restrict msg, void *restrict ptr,
48                           errno_t error)
49 {
50     (void)ptr;
51     (void)error;
52     strcpy(handler_msg, msg);
53 }
54 
55 #define TEST_RES(cond, msg, handler_res, test_id)                              \
56     if ((!(cond)) || (handler_res == 1)) {                                     \
57         printf("Test %d Failed: %s\n", test_id, msg);                          \
58         return 1;                                                              \
59     } else {                                                                   \
60         printf("Test %d Passed: %s\n", test_id, msg);                          \
61     }
62 
63 static int
test_handler_called(int handler_called,char * expected_msg,int test_id)64 test_handler_called(int handler_called, char *expected_msg, int test_id)
65 {
66     int ret = 0;
67     if (handler_called == 0) {
68         (void)expected_msg;
69         if (handler_msg[0] != '\0') {
70             printf(
71                 "ERROR: Custom constraint handler called without error detiction!\n");
72             printf("Test %d Failed: Error msg is incorrect\n", test_id);
73             ret = 1;
74         }
75     } else {
76         if (handler_msg[0] == '\0') {
77             (void)expected_msg;
78             printf("ERROR: Custom constraint handler not called\n");
79             printf("Test %d Failed: Error msg is incorrect\n", test_id);
80             ret = 1;
81         } else {
82             if (strcmp(expected_msg, handler_msg) != 0) {
83                 printf(
84                     "ERROR: Custom constraint handler called with incorrect msg: %s\n",
85                     handler_msg);
86                 printf("Test %d Failed: Error msg is incorrect\n", test_id);
87                 ret = 1;
88             } else {
89                 (void)expected_msg;
90                 printf(
91                     "Custom constraint handler called with correct msg: %s\n",
92                     handler_msg);
93                 handler_msg[0] = '\0';
94                 ret = 0;
95             }
96         }
97     }
98     return ret;
99 }
100 
101 int
main(void)102 main(void)
103 {
104     char buf[50];
105     int test_id = 0;
106     int handler_res = 0;
107     errno_t res;
108 
109     set_constraint_handler_s(custom_constraint_handler);
110 
111     // Test case 1: Normal formatting
112     test_id++;
113     res = sprintf_s(buf, sizeof(buf), "Hello, %s!", "world");
114     handler_res = test_handler_called(0, "", test_id);
115     TEST_RES(res == (int)strlen("Hello, world!"), "Normal formatting",
116              handler_res, test_id);
117     TEST_RES(strcmp(buf, "Hello, world!") == 0, "Normal formatting Contents",
118              handler_res, test_id);
119 
120     // Test case 2: Formatting with buffer overflow
121     test_id++;
122     res = sprintf_s(buf, 10, "Hello, %s!", "world");
123     handler_res =
124         test_handler_called(1, "sprintf_s: dest buffer overflow", test_id);
125     TEST_RES(res == 0, "Formatting with buffer overflow", handler_res, test_id);
126 
127     // Test case 3: Formatting with Null buffer
128     test_id++;
129     res = sprintf_s(NULL, sizeof(buf), "Hello, %s!", "world");
130     handler_res =
131         test_handler_called(1, "sprintf_s: dest buffer is null", test_id);
132     TEST_RES(res == 0, "Formatting with Null buffer", handler_res, test_id);
133 
134     // Test case 4: Formatting with Null format string
135     test_id++;
136     res = sprintf_s(buf, sizeof(buf), NULL, "world");
137     handler_res =
138         test_handler_called(1, "sprintf_s: null format string", test_id);
139     TEST_RES(res == 0, "Formatting with Null format string", handler_res,
140              test_id);
141 
142     // Test case 5: Empty format string
143     test_id++;
144     res = sprintf_s(buf, sizeof(buf), "", "world");
145     TEST_RES(res == 0, "Empty format string", handler_res, test_id);
146     handler_res = test_handler_called(0, "", test_id);
147     TEST_RES(strcmp(buf, "") == 0, "Empty format string Contents", handler_res,
148              test_id);
149 
150     printf("All sprintf_s tests passed!\n");
151     return 0;
152 }
153