File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed
autosar/test/rules/A4-7-1
common/src/codingstandards/cpp Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 10
10
| test.cpp:22:12:22:16 | ... + ... | Binary expression ...+... may overflow. |
11
11
| test.cpp:50:7:50:14 | ... + ... | Binary expression ...+... may overflow. |
12
12
| test.cpp:62:8:62:10 | ... ++ | Binary expression ...++... may overflow. |
13
+ | test.cpp:91:10:91:17 | ... << ... | Binary expression ...<<... may overflow. |
14
+ | test.cpp:95:10:95:17 | ... << ... | Binary expression ...<<... may overflow. |
15
+ | test.cpp:98:8:98:15 | ... << ... | Binary expression ...<<... may overflow. |
Original file line number Diff line number Diff line change @@ -72,4 +72,29 @@ void test_pointer() {
72
72
int *p = nullptr ;
73
73
p++; // COMPLIANT - not covered by this rule
74
74
p--; // COMPLIANT - not covered by this rule
75
+ }
76
+
77
+ extern unsigned int popcount (unsigned int );
78
+ #define PRECISION (x ) popcount(x)
79
+ void test_guarded_shifts (unsigned int p1, int p2) {
80
+ unsigned int l1;
81
+
82
+ if (p2 < popcount (p1) && p2 > 0 ) {
83
+ l1 = p1 << p2; // COMPLIANT
84
+ }
85
+
86
+ if (p2 < PRECISION (p1) && p2 > 0 ) {
87
+ l1 = p1 << p2; // COMPLIANT
88
+ }
89
+
90
+ if (p2 < popcount (p1)) {
91
+ l1 = p1 << p2; // NON_COMPLIANT - p2 could be negative
92
+ }
93
+
94
+ if (p2 > 0 ) {
95
+ l1 = p1 << p2; // NON_COMPLIANT - p2 could have a higher precision
96
+ }
97
+
98
+ l1 = p1 << p2; // NON_COMPLIANT - p2 may have a higher precision or could be
99
+ // negative
75
100
}
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ import SimpleRangeAnalysisCustomizations
8
8
import semmle.code.cpp.controlflow.Guards
9
9
import codingstandards.cpp.dataflow.TaintTracking
10
10
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
11
+ import codingstandards.cpp.Expr
12
+ import codingstandards.cpp.UndefinedBehavior
11
13
12
14
/**
13
15
* An integer operation that may overflow, underflow or wrap.
@@ -40,7 +42,9 @@ class InterestingOverflowingOperation extends Operation {
40
42
// Not within a macro
41
43
not this .isAffectedByMacro ( ) and
42
44
// Ignore pointer arithmetic
43
- not this instanceof PointerArithmeticOperation
45
+ not this instanceof PointerArithmeticOperation and
46
+ // In case of the shift operation, it must cause undefined behavior
47
+ ( this instanceof BitShiftExpr implies this instanceof ShiftByNegativeOrGreaterPrecisionOperand )
44
48
}
45
49
46
50
/**
You can’t perform that action at this time.
0 commit comments