1+ #include <stddef.h>
2+ #include <string.h>
3+
4+ int * restrict g1 ;
5+ int * restrict g2 ;
6+
7+ void test_global_local () {
8+ int * restrict i1 = g1 ; // COMPLIANT
9+ int * restrict i2 = g2 ; // COMPLIANT
10+ int * restrict i3 = i2 ; // NON_COMPLIANT
11+ g1 = g2 ; // NON_COMPLIANT
12+ i1 = i2 ; // NON_COMPLIANT
13+ }
14+
15+ void copy (int * restrict p1 , int * restrict p2 , size_t s ) {
16+ for (size_t i = 0 ; i < s ; ++ i ) {
17+ p2 [i ] = p1 [i ];
18+ }
19+ }
20+
21+ void test_restrict_params () {
22+ int i1 = 1 ;
23+ int i2 = 2 ;
24+ copy (& i1 , & i1 , 1 ); // NON_COMPLIANT
25+ copy (& i1 , & i2 , 1 ); // COMPLIANT
26+
27+ int x [10 ];
28+ copy (x [0 ], x [1 ], 1 ); // COMPLIANT - non overlapping
29+ copy (x [0 ], x [1 ], 2 ); // NON_COMPLIANT - overlapping
30+ }
31+
32+ void test_strcpy () {
33+ char s1 [] = "my test string" ;
34+ char s2 [] = "my other string" ;
35+ strcpy (& s1 , & s1 + 3 ); // NON_COMPLIANT
36+ strcpy (& s2 , & s1 ); // COMPLIANT
37+ }
38+
39+ void test_strcpy_s () {
40+ char s1 [] = "my test string" ;
41+ char s2 [] = "my other string" ;
42+ strcpy_s (& s1 , & s1 + 3 ); // NON_COMPLIANT
43+ strcpy_s (& s2 , sizeof (s2 ), & s1 ); // COMPLIANT
44+ }
45+
46+ void test_memcpy () {
47+ char s1 [] = "my test string" ;
48+ char s2 [] = "my other string" ;
49+ memcpy (& s1 , & s1 + 3 , 5 ); // NON_COMPLIANT
50+ memcpy (& s2 , & s1 + 3 , 5 ); // COMPLIANT
51+ }
52+
53+ void test_memcpy_s () {
54+ char s1 [] = "my test string" ;
55+ char s2 [] = "my other string" ;
56+ memcpy_s (& s1 , sizeof (s1 ), & s1 + 3 , 5 ); // NON_COMPLIANT
57+ memcpy_s (& s2 , sizeof (s2 ), & s1 + 3 , 5 ); // COMPLIANT
58+ }
59+
60+ void test_memmove () {
61+ char s1 [] = "my test string" ;
62+ char s2 [] = "my other string" ;
63+ memmove (& s1 , & s1 + 3 , 5 ); // COMPLIANT
64+ memmove (& s2 , & s1 + 3 , 5 ); // COMPLIANT
65+ }
66+
67+ void test_scanf () {
68+ char s1 [200 ] = "%10s" ;
69+ scanf (& s2 , & s2 + 4 ); // NON_COMPLIANT
70+ }
71+
72+ // TODO also consider the following:
73+ // strncpy(), strncpy_s()
74+ // strcat(), strcat_s()
75+ // strncat(), strncat_s()
76+ // strtok_s()
0 commit comments