1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <string.h>
4
+ #include <time.h>
4
5
#include <wchar.h>
5
6
6
- char * get_ca_5 (void ) { return malloc (5 * sizeof (char )); }
7
-
8
- char * get_ca_5_zeroed (void ) {
9
- char * p = malloc (5 * sizeof (char ));
10
- memset (p , 0 , 5 * sizeof (char ));
7
+ char * get_ca_5 (void ) {
8
+ void * ptr = malloc (5 * sizeof (char ));
9
+ memset (ptr , 0 , 5 * sizeof (char ));
10
+ return (char * )ptr ;
11
11
}
12
12
13
- int compare (void * , void * ) {}
13
+ int compare (void * a , void * b ) {}
14
+
15
+ void test_strings_loop (void ) {
16
+ char ca5 [5 ] = "test" ; // ok
17
+ char buf5 [5 ] = {0 };
18
+
19
+ for (int i = 0 ; i < 5 ; i ++ ) {
20
+ strcpy (buf5 , ca5 ); // COMPLIANT
21
+ strcpy (buf5 + i , ca5 ); // NON_COMPLIANT[FALSE_NEGATIVE]
22
+ strncpy (buf5 , ca5 , i ); // COMPLIANT
23
+ strncpy (buf5 , ca5 , i + 1 ); // NON_COMPLIANT[FALSE_NEGATIVE]
24
+ }
25
+ }
14
26
15
27
void test_strings (int flow , int unk_size ) {
16
28
char ca5_good [5 ] = "test" ; // ok
@@ -95,7 +107,7 @@ void test_strings(int flow, int unk_size) {
95
107
} // COMPLIANT
96
108
if (flow ) {
97
109
strncpy (ca6_bad , ca5_good , 6 );
98
- } // NON_COMPLIANT[FALSE_POSITIVE]
110
+ } // COMPLIANT
99
111
if (flow ) {
100
112
strncpy (ca5_good + 1 , ca5_good + 2 , 3 );
101
113
} // COMPLIANT
@@ -132,7 +144,7 @@ void test_strings(int flow, int unk_size) {
132
144
char buf3 [10 ] = {'\0' };
133
145
char buf4 [10 ] = "12345" ;
134
146
135
- strcat (buf0 , " " ); // COMPLIANT [FALSE_NEGATIVE] - not null terminated at
147
+ strcat (buf0 , " " ); // NON_COMPLIANT [FALSE_NEGATIVE] - not null terminated at
136
148
// initialization
137
149
138
150
memset (buf0 , 0 , sizeof (buf0 )); // COMPLIANT
@@ -156,25 +168,22 @@ void test_strings(int flow, int unk_size) {
156
168
wchar_t buf3 [10 ] = {L'\0' };
157
169
wchar_t buf4 [10 ] = L"12345" ;
158
170
159
- wcsncat (
160
- buf0 , L" " ,
161
- 1 ); // COMPLIANT[FALSE_NEGATIVE] - not null terminated at initialization
171
+ wcsncat (buf0 , L" " ,
172
+ 1 ); // NON_COMPLIANT[FALSE_NEGATIVE] - not null terminated at
173
+ // initialization
162
174
163
175
memset (buf0 , 0 , sizeof (buf0 )); // COMPLIANT
164
176
memset (buf2 , 0 , sizeof (buf2 )); // COMPLIANT
165
177
166
178
wcsncat (buf1 , L" " , 1 ); // NON_COMPLIANT - not null terminated
167
179
wcsncat (buf2 , L" " , 1 ); // COMPLIANT
168
180
wcsncat (buf3 , L" " , 1 ); // COMPLIANT
169
- wcsncat (buf4 , L"12345" , 5 ); // NON_COMPLIANT
170
-
171
- wcsncat (get_ca_5_zeroed (), L"12345" , 5 ); // NON_COMPLIANT
172
- wcsncat (get_ca_5_zeroed (), L"1234" , 4 ); // NON_COMPLIANT
173
- wcsncat (get_ca_5_zeroed () + 1 , L"1234" , 4 ); // NON_COMPLIANT
174
- wcsncat (get_ca_5_zeroed (), L"12" ,
175
- 2 ); // NON_COMPLIANT - 4 (bytes) + 2 (null term) copied
176
- wcsncat (get_ca_5_zeroed () + 1 , L"1" ,
177
- 1 ); // COMPLIANT - 2 (bytes) + 2 (null term) copied
181
+ wcsncat (buf4 , L"12345" , 5 ); // NON_COMPLIANT[FALSE_NEGATIVE]
182
+
183
+ wcsncat (get_ca_5 (), L"12345" , 5 ); // NON_COMPLIANT
184
+ wcsncat (get_ca_5 (), L"1234" , 4 ); // NON_COMPLIANT
185
+ wcsncat (get_ca_5 () + 1 , L"1234" , 4 ); // NON_COMPLIANT
186
+ wcsncat (get_ca_5 (), L"12" , 2 ); // NON_COMPLIANT
178
187
}
179
188
180
189
// strcmp
@@ -189,7 +198,8 @@ void test_strings(int flow, int unk_size) {
189
198
// strncmp
190
199
if (flow ) {
191
200
strncmp (ca5_good , ca5_bad , 4 ); // COMPLIANT
192
- strncmp (ca5_good , ca5_bad , 5 ); // NON_COMPLIANT
201
+ strncmp (ca5_good , ca5_bad , 5 ); // COMPLIANT
202
+ strncmp (ca5_good , ca5_bad , 6 ); // NON_COMPLIANT
193
203
}
194
204
}
195
205
@@ -201,7 +211,7 @@ void test_wrong_buf_size(void) {
201
211
fgets (buf , sizeof (buf ), stdin ); // COMPLIANT
202
212
fgets (buf , sizeof (buf ) - 1 , stdin ); // COMPLIANT
203
213
fgets (buf , sizeof (buf ) + 1 , stdin ); // NON_COMPLIANT
204
- fgets (buf , 0 , stdin ); // NON_COMPLIANT
214
+ fgets (buf , 0 , stdin ); // COMPLIANT
205
215
fgets (buf + 1 , sizeof (buf ) - 1 , stdin ); // COMPLIANT
206
216
fgets (buf + 1 , sizeof (buf ), stdin ); // NON_COMPLIANT
207
217
}
@@ -213,8 +223,7 @@ void test_wrong_buf_size(void) {
213
223
fgetws (wbuf , sizeof (wbuf ) / sizeof (* wbuf ), stdin ); // COMPLIANT
214
224
fgetws (wbuf , sizeof (wbuf ) / sizeof (* wbuf ) - 1 , stdin ); // COMPLIANT
215
225
fgetws (wbuf , sizeof (wbuf ) / sizeof (* wbuf ) + 1 , stdin ); // NON_COMPLIANT
216
- fgetws (wbuf , 0 , stdin ); // NON_COMPLIANT
217
- fgetws (wbuf + 1 , sizeof (wbuf ) / sizeof (* wbuf ) - 1 , stdin ); // NON_COMPLIANT
226
+ fgetws (wbuf , 0 , stdin ); // COMPLIANT
218
227
fgetws (wbuf + 1 , sizeof (wbuf ) / sizeof (* wbuf ) - 2 , stdin ); // COMPLIANT
219
228
fgetws (wbuf + 1 , sizeof (wbuf ) / sizeof (* wbuf ), stdin ); // NON_COMPLIANT
220
229
}
@@ -246,7 +255,7 @@ void test_wrong_buf_size(void) {
246
255
// mbtowc
247
256
{
248
257
wchar_t c ;
249
- char buf [2 ];
258
+ char buf [2 ] = { 0 } ;
250
259
mbtowc (& c , buf , sizeof (buf )); // COMPLIANT
251
260
mbtowc (& c , buf , sizeof (buf ) - 1 ); // COMPLIANT
252
261
mbtowc (& c , buf , sizeof (buf ) + 1 ); // NON_COMPLIANT
@@ -255,7 +264,7 @@ void test_wrong_buf_size(void) {
255
264
256
265
// mblen
257
266
{
258
- char buf [3 ];
267
+ char buf [3 ] = { 0 } ;
259
268
mblen (buf , sizeof (buf )); // COMPLIANT
260
269
mblen (buf , sizeof (buf ) + 1 ); // NON_COMPLIANT
261
270
mblen ((char * )malloc (5 ), sizeof (buf ) * 2 ); // NON_COMPLIANT
@@ -302,10 +311,11 @@ void test_wrong_buf_size(void) {
302
311
{
303
312
char buf [64 ];
304
313
char buf2 [128 ];
305
- strxfrm (buf , "abc" , sizeof (buf )); // COMPLIANT
306
- strxfrm (buf , "abc" , sizeof (buf ) + 1 ); // NON_COMPLIANT
307
- strxfrm (buf , "abc" , sizeof (buf ) - 1 ); // COMPLIANT
308
- strxfrm (buf + 1 , buf2 , sizeof (buf ) - 1 ); // COMPLIANT
314
+ strxfrm (buf , "abc" , sizeof (buf )); // COMPLIANT
315
+ strxfrm (buf , "abc" , sizeof (buf ) + 1 ); // NON_COMPLIANT
316
+ strxfrm (buf , "abc" , sizeof (buf ) - 1 ); // COMPLIANT
317
+ strxfrm (buf + 1 , buf2 ,
318
+ sizeof (buf ) - 1 ); // NON_COMPLIANT - not null terminated
309
319
}
310
320
311
321
// wcsxfrm
0 commit comments