33#include <stdint.h>
44
55void test_add_simple (signed int i1 , signed int i2 ) {
6- i1 + i2 ; // NON_COMPLIANT - not bounds checked
6+ i1 + i2 ; // NON_COMPLIANT - not bounds checked
7+ i1 += i2 ; // NON_COMPLIANT - not bounds checked
78}
89
910void test_add_precheck (signed int i1 , signed int i2 ) {
@@ -12,15 +13,17 @@ void test_add_precheck(signed int i1, signed int i2) {
1213 ((i2 < 0 ) && (i1 < (INT_MIN - i2 )))) {
1314 // handle error
1415 } else {
15- i1 + i2 ; // COMPLIANT - bounds appropriately checked
16+ i1 + i2 ; // COMPLIANT - bounds appropriately checked
17+ i1 += i2 ; // COMPLIANT - bounds appropriately checked
1618 }
1719}
1820
1921void test_add_precheck_2 (signed int i1 , signed int i2 ) {
2022 if (i1 + i2 < i1 ) { // NON_COMPLIANT - bad overflow check - undefined behavior
2123 // handle error
2224 } else {
23- i1 + i2 ; // NON_COMPLIANT
25+ i1 + i2 ; // NON_COMPLIANT
26+ i1 += i2 ; // NON_COMPLIANT
2427 }
2528}
2629
@@ -30,18 +33,24 @@ void test_add_postcheck(signed int i1, signed int i2) {
3033 if (i3 < i1 ) {
3134 // handle error
3235 }
36+ i1 += i2 ; // NON_COMPLIANT
37+ if (i1 < i2 ) {
38+ // handle error
39+ }
3340}
3441
3542void test_sub_simple (signed int i1 , signed int i2 ) {
36- i1 - i2 ; // NON_COMPLIANT - not bounds checked
43+ i1 - i2 ; // NON_COMPLIANT - not bounds checked
44+ i1 -= i2 ; // NON_COMPLIANT - not bounds checked
3745}
3846
3947void test_sub_precheck (signed int i1 , signed int i2 ) {
4048 // Style recomended by CERT
4149 if ((i2 > 0 && i1 < INT_MIN + i2 ) || (i2 < 0 && i1 > INT_MAX + i2 )) {
4250 // handle error
4351 } else {
44- i1 - i2 ; // COMPLIANT - bounds checked
52+ i1 - i2 ; // COMPLIANT - bounds checked
53+ i1 -= i2 ; // COMPLIANT - bounds checked
4554 }
4655}
4756
@@ -50,10 +59,15 @@ void test_sub_postcheck(signed int i1, signed int i2) {
5059 if (i3 > i1 ) {
5160 // handle error
5261 }
62+ i1 -= i2 ; // NON_COMPLIANT - underflow is undefined behavior.
63+ if (i1 > i2 ) {
64+ // handle error
65+ }
5366}
5467
5568void test_mul_simple (signed int i1 , signed int i2 ) {
56- i1 * i2 ; // NON_COMPLIANT
69+ i1 * i2 ; // NON_COMPLIANT
70+ i1 *= i2 ; // NON_COMPLIANT
5771}
5872
5973void test_mul_precheck (signed int i1 , signed int i2 ) {
@@ -66,6 +80,7 @@ void test_mul_precheck(signed int i1, signed int i2) {
6680 } else {
6781 i1 * i2 ; // COMPLIANT - checked
6882 result = (signed int )tmp ;
83+ i1 *= i2 ; // COMPLIANT - checked
6984 }
7085}
7186
@@ -94,43 +109,49 @@ void test_mul_precheck_2(signed int i1, signed int i2) {
94109 }
95110 }
96111 }
97- i1 * i2 ; // COMPLIANT
112+ i1 * i2 ; // COMPLIANT
113+ i1 *= i2 ; // COMPLIANT
98114}
99115
100116void test_simple_div (signed int i1 , signed int i2 ) {
101117 if (i2 == 0 ) {
102118 // handle error
103119 } else {
104- i1 / i2 ; // NON_COMPLIANT
120+ i1 / i2 ; // NON_COMPLIANT
121+ i1 /= i2 ; // NON_COMPLIANT
105122 }
106123}
107124
108125void test_div_precheck (signed int i1 , signed int i2 ) {
109126 if ((i2 == 0 ) || ((i1 == LONG_MIN ) && (i2 == -1 ))) {
110127 /* Handle error */
111128 } else {
112- i1 / i2 ; // COMPLIANT
129+ i1 / i2 ; // COMPLIANT
130+ i1 /= i2 ; // COMPLIANT
113131 }
114132}
115133
116134void test_simple_rem (signed int i1 , signed int i2 ) {
117135 if (i2 == 0 ) {
118136 // handle error
119137 } else {
120- i1 % i2 ; // NON_COMPLIANT
138+ i1 % i2 ; // NON_COMPLIANT
139+ i1 %= i2 ; // NON_COMPLIANT
121140 }
122141}
123142
124143void test_rem_precheck (signed int i1 , signed int i2 ) {
125144 if ((i2 == 0 ) || ((i1 == LONG_MIN ) && (i2 == -1 ))) {
126145 /* Handle error */
127146 } else {
128- i1 % i2 ; // COMPLIANT
147+ i1 % i2 ; // COMPLIANT
148+ i1 %= i2 ; // COMPLIANT
129149 }
130150}
131151
132152void test_simple_left_shift (signed int i1 , signed int i2 ) {
133- i1 << i2 ; // NON_COMPLIANT
153+ i1 << i2 ; // NON_COMPLIANT
154+ i1 <<= i2 ; // NON_COMPLIANT
134155}
135156
136157/* Returns the number of set bits */
@@ -143,7 +164,8 @@ void test_left_shift_precheck(signed int i1, signed int i2) {
143164 (i1 > (INT_MAX >> i2 ))) {
144165 // handle error
145166 } else {
146- i1 << i2 ; // COMPLIANT
167+ i1 << i2 ; // COMPLIANT
168+ i1 <<= i2 ; // COMPLIANT
147169 }
148170}
149171
0 commit comments