Skip to content

Commit 614a802

Browse files
Merge remote-tracking branch 'origin/michaelrfairhurst/package-undefined-behavior' into michaelrfairhurst/package-undefined-behavior-mismatched-list-delete
2 parents e57263b + 992b672 commit 614a802

27 files changed

+484
-64
lines changed

c/cert/src/rules/INT32-C/SignedIntegerOverflow.ql

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,10 @@
1919

2020
import cpp
2121
import codingstandards.c.cert
22-
import codingstandards.cpp.Overflow
23-
import semmle.code.cpp.controlflow.Guards
24-
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
22+
import codingstandards.cpp.rules.signedintegeroverflowshared.SignedIntegerOverflowShared
2523

26-
from InterestingOverflowingOperation op
27-
where
28-
not isExcluded(op, IntegerOverflowPackage::signedIntegerOverflowQuery()) and
29-
(
30-
// An operation that returns a signed integer type
31-
op.getType().getUnderlyingType().(IntegralType).isSigned()
32-
or
33-
// The divide or rem expression on a signed integer
34-
op.(DivOrRemOperation).getDividend().getType().getUnderlyingType().(IntegralType).isSigned()
35-
) and
36-
// Not checked before the operation
37-
not op.hasValidPreCheck() and
38-
// Covered by INT34-C
39-
not op instanceof LShiftExpr
40-
select op,
41-
"Operation " + op.getOperator() + " of type " + op.getType().getUnderlyingType() +
42-
" may overflow or underflow."
24+
module SignedIntegerOverflowConfig implements SignedIntegerOverflowSharedConfigSig {
25+
Query getQuery() { result = IntegerOverflowPackage::signedIntegerOverflowQuery() }
26+
}
27+
28+
import SignedIntegerOverflowShared<SignedIntegerOverflowConfig>

c/cert/test/rules/INT32-C/SignedIntegerOverflow.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+
c/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.ql

c/cert/test/rules/INT32-C/SignedIntegerOverflow.expected renamed to c/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
| test.c:147:5:147:12 | ... %= ... | Operation %= of type signed int may overflow or underflow. |
2222
| test.c:161:3:161:5 | - ... | Operation - of type signed int may overflow or underflow. |
2323
| test.c:173:3:173:6 | ... ++ | Operation ++ of type signed int may overflow or underflow. |
24-
| test.c:189:3:189:6 | ... -- | Operation -- of type signed int may overflow or underflow. |
24+
| test.c:189:3:189:6 | ... -- | Operation -- of type signed int may overflow or underflow. |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.signedintegeroverflowshared.SignedIntegerOverflowShared
3+
4+
module TestFileConfig implements SignedIntegerOverflowSharedConfigSig {
5+
Query getQuery() { result instanceof TestQuery }
6+
}
7+
8+
import SignedIntegerOverflowShared<TestFileConfig>

c/cert/test/rules/INT32-C/test.c renamed to c/common/test/rules/signedintegeroverflowshared/test.c

File renamed without changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `INT32-C` - `SignedIntegerOverflow.ql`:
2+
- Refactored query logic into a shared library (`SignedIntegerOverflowShared.qll`) to enable reuse by MISRA C++ `RULE-4-1-3`. The query logic is unchanged and no visible changes to results or performance are expected.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `INT50-CPP` - `DoNotCastToAnOutOfRangeEnumerationValue.ql`:
2+
- Refactored query logic into a shared library (`DoNotCastToAnOutOfRangeEnumerationValueShared.qll`) to enable reuse by MISRA C++ `RULE-4-1-3`. The query logic is unchanged and no visible changes to results or performance are expected.

cpp/cert/src/rules/INT50-CPP/DoNotCastToAnOutOfRangeEnumerationValue.ql

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,12 @@
1818

1919
import cpp
2020
import codingstandards.cpp.cert
21-
import codingstandards.cpp.Enums
22-
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
23-
import codingstandards.cpp.SimpleRangeAnalysisCustomizations
21+
import codingstandards.cpp.rules.donotcasttoanoutofrangeenumerationvalueshared.DoNotCastToAnOutOfRangeEnumerationValueShared
2422

25-
from Cast c, Enum e, string description
26-
where
27-
not isExcluded(c, TypeRangesPackage::doNotCastToAnOutOfRangeEnumerationValueQuery()) and
28-
// Conversion from an integral type to an enum type
29-
c.getExpr().getType().getUnspecifiedType() instanceof IntegralType and
30-
c.getType().getUnspecifiedType() = e and
31-
not (
32-
// The deduced bound for the expression is within the type range for the explicit type
33-
upperBound(c.getExpr()) <= Enums::getValueRangeUpperBound(e) and
34-
lowerBound(c.getExpr()) >= Enums::getValueRangeLowerBound(e)
35-
) and
36-
// Not a compile time constant with the same value as an existing enum constant
37-
not exists(float enumConstantValue |
38-
enumConstantValue = Enums::getEnumConstantValue(e.getAnEnumConstant())
39-
|
40-
// Expression is a constant
41-
c.getExpr().getValue().toFloat() = enumConstantValue
42-
or
43-
// Range analysis has precise bounds
44-
enumConstantValue = upperBound(c.getExpr()) and
45-
enumConstantValue = lowerBound(c.getExpr())
46-
) and
47-
(
48-
if exists(upperBound(c.getExpr()))
49-
then
50-
description =
51-
"Cast to enum $@ with value range " + Enums::getValueRangeLowerBound(e) + "..." +
52-
Enums::getValueRangeUpperBound(e) + " from expression with wider value range " +
53-
lowerBound(c.getExpr()) + "..." + upperBound(c.getExpr()) + " in function " +
54-
c.getEnclosingFunction().getName() + "."
55-
else
56-
description =
57-
"Cast to enum $@ with value range " + Enums::getValueRangeLowerBound(e) + "..." +
58-
Enums::getValueRangeUpperBound(e) +
59-
" from expression with a potentially wider value range."
60-
)
61-
select c, description, e, e.getName()
23+
module DoNotCastToAnOutOfRangeEnumerationValueConfig implements
24+
DoNotCastToAnOutOfRangeEnumerationValueSharedConfigSig
25+
{
26+
Query getQuery() { result = TypeRangesPackage::doNotCastToAnOutOfRangeEnumerationValueQuery() }
27+
}
28+
29+
import DoNotCastToAnOutOfRangeEnumerationValueShared<DoNotCastToAnOutOfRangeEnumerationValueConfig>

cpp/cert/test/rules/INT50-CPP/DoNotCastToAnOutOfRangeEnumerationValue.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)