Skip to content

Commit 195cc3f

Browse files
committed
IntegerOverflow: Implement Rule 12.4.
Implement Rule 12.4 by sharing a query with M5-19-1 for finding constant integer expressions that wrap around.
1 parent c4ff2de commit 195cc3f

File tree

12 files changed

+126
-9
lines changed

12 files changed

+126
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
| test.c:11:7:11:18 | ... - ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
2+
| test.c:12:7:12:18 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
3+
| test.c:18:7:18:19 | ... - ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
4+
| test.c:19:7:19:19 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
5+
| test.c:25:7:25:18 | ... - ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
6+
| test.c:26:7:26:17 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
7+
| test.c:33:7:33:20 | ... - ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
8+
| test.c:34:7:34:16 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
9+
| test.c:34:7:34:20 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
10+
| test.c:37:40:37:49 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
11+
| test.c:40:7:40:19 | ... - ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
12+
| test.c:41:7:41:19 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
13+
| test.c:46:7:46:17 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
14+
| test.c:47:7:47:18 | ... - ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
15+
| test.c:48:7:48:17 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
16+
| test.c:48:7:48:17 | ... + ... | Use of a constant, unsigned, integer expression that over- or under-flows. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.constantunsignedintegerexpressionswraparound.ConstantUnsignedIntegerExpressionsWrapAround
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <limits.h>
2+
3+
// UINT_MIN and UULONG_MIN isn't defined, but it's going to be zero
4+
#define UINT_MIN ((unsigned int)0)
5+
#define UULONG_MIN ((unsigned long long)0)
6+
7+
void test_signed_int() {
8+
unsigned int a;
9+
a = 1 + 1; // COMPLIANT
10+
a = 0 - 1; // COMPLIANT
11+
a = UINT_MIN - 1; // NON_COMPLIANT
12+
a = UINT_MAX + 1; // NON_COMPLIANT
13+
14+
const unsigned int const_min = UINT_MIN;
15+
const unsigned int const_max = UINT_MAX;
16+
a = const_min + 1; // COMPLIANT
17+
a = const_max - 1; // COMPLIANT
18+
a = const_min - 1; // NON_COMPLIANT
19+
a = const_max + 1; // NON_COMPLIANT
20+
21+
#define UNDERFLOW(x) (UINT_MIN - (x))
22+
#define OVERFLOW(x) (UINT_MAX + (x))
23+
a = UNDERFLOW(0); // COMPLIANT
24+
a = OVERFLOW(0); // COMPLIANT
25+
a = UNDERFLOW(1); // NON_COMPLIANT
26+
a = OVERFLOW(1); // NON_COMPLIANT
27+
}
28+
29+
void test_long_long() {
30+
unsigned long long a;
31+
a = 1 + 1; // COMPLIANT
32+
a = 0 - 1; // COMPLIANT
33+
a = UULONG_MIN - 1; // NON_COMPLIANT
34+
a = ULLONG_MAX + 1; // NON_COMPLIANT
35+
36+
const unsigned long long const_min = UULONG_MIN;
37+
const unsigned long long const_max = ULLONG_MAX;
38+
a = const_min + 1; // COMPLIANT
39+
a = const_max - 1; // COMPLIANT
40+
a = const_min - 1; // NON_COMPLIANT
41+
a = const_max + 1; // NON_COMPLIANT
42+
43+
#define UNDERFLOW(x) (UULONG_MIN - (x))
44+
#define OVERFLOW(x) (ULLONG_MAX + (x))
45+
a = UNDERFLOW(0); // COMPLIANT
46+
a = OVERFLOW(0); // COMPLIANT
47+
a = UNDERFLOW(1); // NON_COMPLIANT
48+
a = OVERFLOW(1); // NON_COMPLIANT
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/constant-unsigned-integer-expressions-wrap-around
3+
* @name RULE-12-4: Evaluation of constant expressions should not lead to unsigned integer wrap-around
4+
* @description Unsigned integer expressions do not strictly overflow, but instead wrap around in a
5+
* modular way. Any constant unsigned integer expressions that in effect "overflow"
6+
* will not be detected by the compiler. Although there may be good reasons at run-time
7+
* to rely on the modular arithmetic provided by unsigned integer types, the reasons
8+
* for using it at compile-time to evaluate a constant expression are less obvious. Any
9+
* instance of an unsigned integer constant expression wrapping around is therefore
10+
* likely to indicate a programming error.
11+
* @kind problem
12+
* @precision very-high
13+
* @problem.severity error
14+
* @tags external/misra/id/rule-12-4
15+
* correctness
16+
* security
17+
* external/misra/obligation/advisory
18+
*/
19+
20+
import cpp
21+
import codingstandards.c.misra
22+
import codingstandards.cpp.rules.constantunsignedintegerexpressionswraparound.ConstantUnsignedIntegerExpressionsWrapAround
23+
24+
class ConstantUnsignedIntegerExpressionsWrapAroundQuery extends ConstantUnsignedIntegerExpressionsWrapAroundSharedQuery {
25+
ConstantUnsignedIntegerExpressionsWrapAroundQuery() {
26+
this = IntegerOverflowPackage::constantUnsignedIntegerExpressionsWrapAroundQuery()
27+
}
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/constantunsignedintegerexpressionswraparound/ConstantUnsignedIntegerExpressionsWrapAround.ql

cpp/autosar/src/rules/M5-19-1/ConstantUnsignedIntegerExpressionsWrapAround.ql

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121

2222
import cpp
2323
import codingstandards.cpp.autosar
24-
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
24+
import codingstandards.cpp.rules.constantunsignedintegerexpressionswraparound.ConstantUnsignedIntegerExpressionsWrapAround
2525

26-
from BinaryArithmeticOperation bao
27-
where
28-
not isExcluded(bao, ExpressionsPackage::constantUnsignedIntegerExpressionsWrapAroundQuery()) and
29-
bao.isConstant() and
30-
bao.getFullyConverted().getUnderlyingType().(IntegralType).isUnsigned() and
31-
convertedExprMightOverflow(bao)
32-
select bao, "Use of a constant, unsigned, integer expression that over- or under-flows."
26+
class ConstantUnsignedIntegerExpressionsWrapAroundQuery extends ConstantUnsignedIntegerExpressionsWrapAroundSharedQuery {
27+
ConstantUnsignedIntegerExpressionsWrapAroundQuery() {
28+
this = ExpressionsPackage::constantUnsignedIntegerExpressionsWrapAroundQuery()
29+
}
30+
}

cpp/autosar/test/rules/M5-19-1/ConstantUnsignedIntegerExpressionsWrapAround.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/constantunsignedintegerexpressionswraparound/ConstantUnsignedIntegerExpressionsWrapAround.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Provides a library which includes a `problems` predicate for reporting unsigned integer
3+
* wraparound related to constant expressions.
4+
*/
5+
6+
import cpp
7+
import codingstandards.cpp.Customizations
8+
import codingstandards.cpp.Exclusions
9+
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
10+
11+
abstract class ConstantUnsignedIntegerExpressionsWrapAroundSharedQuery extends Query { }
12+
13+
Query getQuery() { result instanceof ConstantUnsignedIntegerExpressionsWrapAroundSharedQuery }
14+
15+
query predicate problems(BinaryArithmeticOperation bao, string message) {
16+
not isExcluded(bao, getQuery()) and
17+
bao.isConstant() and
18+
bao.getFullyConverted().getUnderlyingType().(IntegralType).isUnsigned() and
19+
convertedExprMightOverflow(bao) and
20+
message = "Use of a constant, unsigned, integer expression that over- or under-flows."
21+
}

0 commit comments

Comments
 (0)