1+ #include <stddef.h>
2+ #include <stdio.h>
3+ #include <string.h>
4+
5+ int * restrict g1 ;
6+ int * restrict g2 ;
7+ int * restrict g1_1 ;
8+ int * g2_1 ;
9+
10+ struct s1 {
11+ int x , y , z ;
12+ };
13+ struct s1 v1 ;
14+
15+ void test_global_local () {
16+ int * restrict i1 = g1 ; // COMPLIANT
17+ int * restrict i2 = g2 ; // COMPLIANT
18+ int * restrict i3 = i2 ; // NON_COMPLIANT
19+ g1 = g2 ; // NON_COMPLIANT
20+ i1 = i2 ; // NON_COMPLIANT
21+ {
22+ int * restrict i4 ;
23+ int * restrict i5 ;
24+ int * restrict i6 ;
25+ i4 = g1 ; // COMPLIANT
26+ i4 = (void * )0 ; // COMPLIANT
27+ i5 = g1 ; // NON_COMPLIANT - block rather than statement scope matters
28+ i4 = g1 ; // NON_COMPLIANT
29+ i6 = g2 ; // COMPLIANT
30+ }
31+ }
32+
33+ void test_global_local_1 () {
34+ g1_1 = g2_1 ; // COMPLIANT
35+ }
36+
37+ void test_structs () {
38+ struct s1 * restrict p1 = & v1 ;
39+ int * restrict px = & v1 .x ; // NON_COMPLIANT
40+ {
41+ int * restrict py ;
42+ int * restrict pz ;
43+ py = & v1 .y ; // COMPLIANT
44+ py = (int * )0 ;
45+ pz = & v1 .z ; // NON_COMPLIANT - block rather than statement scope matters
46+ py = & v1 .y ; // NON_COMPLIANT
47+ }
48+ }
49+
50+ void copy (int * restrict p1 , int * restrict p2 , size_t s ) {
51+ for (size_t i = 0 ; i < s ; ++ i ) {
52+ p2 [i ] = p1 [i ];
53+ }
54+ }
55+
56+ void test_restrict_params () {
57+ int i1 = 1 ;
58+ int i2 = 2 ;
59+ copy (& i1 , & i1 , 1 ); // NON_COMPLIANT
60+ copy (& i1 , & i2 , 1 ); // COMPLIANT
61+
62+ int x [10 ];
63+ int * px = & x [0 ];
64+ copy (& x [0 ], & x [1 ], 1 ); // COMPLIANT - non overlapping
65+ copy (& x [0 ], & x [1 ], 2 ); // NON_COMPLIANT - overlapping
66+ copy (& x [0 ], (int * )x [0 ], 1 ); // COMPLIANT - non overlapping
67+ copy (& x [0 ], px , 1 ); // NON_COMPLIANT - overlapping
68+ }
69+
70+ void test_strcpy () {
71+ char s1 [] = "my test string" ;
72+ char s2 [] = "my other string" ;
73+ strcpy (& s1 , & s1 + 3 ); // NON_COMPLIANT
74+ strcpy (& s2 , & s1 ); // COMPLIANT
75+ }
76+
77+ void test_memcpy () {
78+ char s1 [] = "my test string" ;
79+ char s2 [] = "my other string" ;
80+ memcpy (& s1 , & s1 + 3 , 5 ); // NON_COMPLIANT
81+ memcpy (& s2 , & s1 + 3 , 5 ); // COMPLIANT
82+ }
83+
84+ void test_memmove () {
85+ char s1 [] = "my test string" ;
86+ char s2 [] = "my other string" ;
87+ memmove (& s1 , & s1 + 3 , 5 ); // COMPLIANT - memmove is allowed to overlap
88+ memmove (& s2 , & s1 + 3 , 5 ); // COMPLIANT
89+ }
90+
91+ void test_scanf () {
92+ char s1 [200 ] = "%10s" ;
93+ scanf (& s1 , & s1 + 4 ); // NON_COMPLIANT
94+ }
95+
96+ // TODO also consider the following:
97+ // strncpy(), strncpy_s()
98+ // strcat(), strcat_s()
99+ // strncat(), strncat_s()
100+ // strtok_s()
0 commit comments