1+ #include <limits.h>
2+
3+ void test_add_simple (unsigned int i1 , unsigned int i2 ) {
4+ i1 + i2 ; // NON_COMPLIANT - not bounds checked
5+ }
6+
7+ void test_add_precheck (unsigned int i1 , unsigned int i2 ) {
8+ if (UINT_MAX - i1 < i2 ) {
9+ // handle error
10+ } else {
11+ i1 + i2 ; // COMPLIANT - bounds checked
12+ }
13+ }
14+
15+ void test_add_precheck_2 (unsigned int i1 , unsigned int i2 ) {
16+ if (i1 + i2 < i1 ) {
17+ // handle error
18+ } else {
19+ i1 + i2 ; // COMPLIANT - bounds checked
20+ }
21+ }
22+
23+ void test_add_postcheck (unsigned int i1 , unsigned int i2 ) {
24+ unsigned int i3 = i1 + i2 ; // COMPLIANT - checked for overflow afterwards
25+ if (i3 < i1 ) {
26+ // handle error
27+ }
28+ }
29+
30+ void test_ex2 (unsigned int i1 , unsigned int i2 ) {
31+ unsigned int ci1 = 2 ;
32+ unsigned int ci2 = 3 ;
33+ ci1 + ci2 ; // COMPLIANT, compile time constants
34+ i1 + 0 ; // COMPLIANT
35+ i1 - 0 ; // COMPLIANT
36+ UINT_MAX - i1 ; // COMPLIANT - cannot be smaller than 0
37+ i1 * 1 ; // COMPLIANT
38+ if (0 <= i1 && i1 < 32 ) {
39+ UINT_MAX >> i1 ; // COMPLIANT
40+ }
41+ }
42+
43+ void test_ex3 (unsigned int i1 , unsigned int i2 ) {
44+ i1 << i2 ; // COMPLIANT - by EX3
45+ }
46+
47+ void test_sub_simple (unsigned int i1 , unsigned int i2 ) {
48+ i1 - i2 ; // NON_COMPLIANT - not bounds checked
49+ }
50+
51+ void test_sub_precheck (unsigned int i1 , unsigned int i2 ) {
52+ if (i1 < i2 ) {
53+ // handle error
54+ } else {
55+ i1 - i2 ; // COMPLIANT - bounds checked
56+ }
57+ }
58+
59+ void test_sub_postcheck (unsigned int i1 , unsigned int i2 ) {
60+ unsigned int i3 = i1 - i2 ; // COMPLIANT - checked for wrap afterwards
61+ if (i3 > i1 ) {
62+ // handle error
63+ }
64+ }
0 commit comments