-
Notifications
You must be signed in to change notification settings - Fork 77
Add critical unspecified behavior for signed integer overflow #1082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaelRFairhurst
merged 4 commits into
michaelrfairhurst/package-undefined-behavior
from
michaelrfairhurst/package-undefined-signed-integer-overflow
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8223495
Add critical unspecified behavior for signed integer overflow
MichaelRFairhurst c8494ab
Format test.cpp
MichaelRFairhurst b51f403
Potential fix for pull request finding
mbaluda e55f1de
Merge branch 'michaelrfairhurst/package-undefined-behavior' into mich…
MichaelRFairhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| c/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
c/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.signedintegeroverflowshared.SignedIntegerOverflowShared | ||
|
|
||
| module TestFileConfig implements SignedIntegerOverflowSharedConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import SignedIntegerOverflowShared<TestFileConfig> |
File renamed without changes.
2 changes: 2 additions & 0 deletions
2
change_notes/2026-03-13-share-signed-integer-overflow-query.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - `INT32-C` - `SignedIntegerOverflow.ql`: | ||
| - 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...src/codingstandards/cpp/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Provides a configurable module SignedIntegerOverflowShared with a `problems` predicate | ||
| * for the following issue: | ||
| * The multiplication of two signed integers can lead to underflow or overflow and | ||
| * therefore undefined behavior. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.Overflow | ||
| import semmle.code.cpp.controlflow.Guards | ||
| import semmle.code.cpp.valuenumbering.GlobalValueNumbering | ||
|
|
||
| signature module SignedIntegerOverflowSharedConfigSig { | ||
| Query getQuery(); | ||
| } | ||
|
|
||
| module SignedIntegerOverflowShared<SignedIntegerOverflowSharedConfigSig Config> { | ||
| query predicate problems(InterestingOverflowingOperation op, string message) { | ||
| not isExcluded(op, Config::getQuery()) and | ||
| ( | ||
| // An operation that returns a signed integer type | ||
| op.getType().getUnderlyingType().(IntegralType).isSigned() | ||
| or | ||
| // The divide or rem expression on a signed integer | ||
| op.(DivOrRemOperation).getDividend().getType().getUnderlyingType().(IntegralType).isSigned() | ||
| ) and | ||
| // Not checked before the operation | ||
| not op.hasValidPreCheck() and | ||
| // Left shift overflow is covered by separate queries (e.g. INT34-C) | ||
| not op instanceof LShiftExpr and | ||
| message = | ||
| "Operation " + op.getOperator() + " of type " + op.getType().getUnderlyingType() + | ||
| " may overflow or underflow." | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
cpp/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| | test.cpp:6:3:6:9 | ... + ... | Operation + of type int may overflow or underflow. | | ||
| | test.cpp:7:3:7:10 | ... += ... | Operation += of type signed int may overflow or underflow. | | ||
| | test.cpp:22:7:22:13 | ... + ... | Operation + of type int may overflow or underflow. | | ||
| | test.cpp:25:5:25:11 | ... + ... | Operation + of type int may overflow or underflow. | | ||
| | test.cpp:26:5:26:12 | ... += ... | Operation += of type signed int may overflow or underflow. | | ||
| | test.cpp:31:19:31:25 | ... + ... | Operation + of type int may overflow or underflow. | | ||
| | test.cpp:36:3:36:10 | ... += ... | Operation += of type signed int may overflow or underflow. | | ||
| | test.cpp:43:3:43:9 | ... - ... | Operation - of type int may overflow or underflow. | | ||
| | test.cpp:44:3:44:10 | ... -= ... | Operation -= of type signed int may overflow or underflow. | | ||
| | test.cpp:58:19:58:25 | ... - ... | Operation - of type int may overflow or underflow. | | ||
| | test.cpp:62:3:62:10 | ... -= ... | Operation -= of type signed int may overflow or underflow. | | ||
| | test.cpp:69:3:69:8 | ... * ... | Operation * of type int may overflow or underflow. | | ||
| | test.cpp:70:3:70:10 | ... *= ... | Operation *= of type signed int may overflow or underflow. | | ||
| | test.cpp:115:3:115:9 | ... / ... | Operation / of type int may overflow or underflow. | | ||
| | test.cpp:116:3:116:10 | ... /= ... | Operation /= of type signed int may overflow or underflow. | | ||
| | test.cpp:123:5:123:11 | ... / ... | Operation / of type int may overflow or underflow. | | ||
| | test.cpp:124:5:124:12 | ... /= ... | Operation /= of type signed int may overflow or underflow. | | ||
| | test.cpp:138:3:138:9 | ... % ... | Operation % of type int may overflow or underflow. | | ||
| | test.cpp:139:3:139:10 | ... %= ... | Operation %= of type signed int may overflow or underflow. | | ||
| | test.cpp:146:5:146:11 | ... % ... | Operation % of type int may overflow or underflow. | | ||
| | test.cpp:147:5:147:12 | ... %= ... | Operation %= of type signed int may overflow or underflow. | | ||
| | test.cpp:161:3:161:5 | - ... | Operation - of type signed int may overflow or underflow. | | ||
| | test.cpp:173:3:173:6 | ... ++ | Operation ++ of type signed int may overflow or underflow. | | ||
| | test.cpp:189:3:189:6 | ... -- | Operation -- of type signed int may overflow or underflow. | |
8 changes: 8 additions & 0 deletions
8
cpp/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.signedintegeroverflowshared.SignedIntegerOverflowShared | ||
|
|
||
| module TestFileConfig implements SignedIntegerOverflowSharedConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import SignedIntegerOverflowShared<TestFileConfig> |
202 changes: 202 additions & 0 deletions
202
cpp/common/test/rules/signedintegeroverflowshared/test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <limits.h> | ||
|
|
||
| void test_add_simple(signed int i1, signed int i2) { | ||
| i1 + i2; // NON_COMPLIANT - not bounds checked | ||
| i1 += i2; // NON_COMPLIANT - not bounds checked | ||
| } | ||
|
|
||
| void test_add_precheck(signed int i1, signed int i2) { | ||
| // Style recommended by CERT | ||
| if (((i2 > 0) && (i1 > (INT_MAX - i2))) || | ||
| ((i2 < 0) && (i1 < (INT_MIN - i2)))) { | ||
| // handle error | ||
| } else { | ||
| i1 + i2; // COMPLIANT - bounds appropriately checked | ||
| i1 += i2; // COMPLIANT - bounds appropriately checked | ||
| } | ||
| } | ||
|
|
||
| void test_add_precheck_2(signed int i1, signed int i2) { | ||
| if (i1 + i2 < i1) { // NON_COMPLIANT - bad overflow check - undefined behavior | ||
| // handle error | ||
| } else { | ||
| i1 + i2; // NON_COMPLIANT | ||
| i1 += i2; // NON_COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_add_postcheck(signed int i1, signed int i2) { | ||
| signed int i3 = i1 + i2; // NON_COMPLIANT - signed overflow is undefined | ||
| // behavior, so checking afterwards is not sufficient | ||
| if (i3 < i1) { | ||
| // handle error | ||
| } | ||
| i1 += i2; // NON_COMPLIANT | ||
| if (i1 < i2) { | ||
| // handle error | ||
| } | ||
| } | ||
|
|
||
| void test_sub_simple(signed int i1, signed int i2) { | ||
| i1 - i2; // NON_COMPLIANT - not bounds checked | ||
| i1 -= i2; // NON_COMPLIANT - not bounds checked | ||
| } | ||
|
|
||
| void test_sub_precheck(signed int i1, signed int i2) { | ||
| // Style recommended by CERT | ||
| if ((i2 > 0 && i1 < INT_MIN + i2) || (i2 < 0 && i1 > INT_MAX + i2)) { | ||
| // handle error | ||
| } else { | ||
| i1 - i2; // COMPLIANT - bounds checked | ||
| i1 -= i2; // COMPLIANT - bounds checked | ||
| } | ||
| } | ||
|
|
||
| void test_sub_postcheck(signed int i1, signed int i2) { | ||
| signed int i3 = i1 - i2; // NON_COMPLIANT - underflow is undefined behavior. | ||
| if (i3 > i1) { | ||
| // handle error | ||
| } | ||
| i1 -= i2; // NON_COMPLIANT - underflow is undefined behavior. | ||
| if (i1 > i2) { | ||
| // handle error | ||
| } | ||
| } | ||
|
|
||
| void test_mul_simple(signed int i1, signed int i2) { | ||
| i1 *i2; // NON_COMPLIANT | ||
| i1 *= i2; // NON_COMPLIANT | ||
| } | ||
|
|
||
| void test_mul_precheck(signed int i1, signed int i2) { | ||
| signed long long tmp = | ||
| (signed long long)i1 * (signed long long)i2; // COMPLIANT | ||
| signed int result; | ||
|
|
||
| if (tmp > INT_MAX || tmp < INT_MIN) { | ||
| // handle error | ||
| } else { | ||
| result = (signed int)tmp; | ||
| } | ||
| } | ||
|
|
||
| void test_mul_precheck_2(signed int i1, signed int i2) { | ||
| if (i1 > 0) { | ||
| if (i2 > 0) { | ||
| if (i1 > (INT_MAX / i2)) { | ||
| return; // handle error | ||
| } | ||
| } else { | ||
| if (i2 < (INT_MIN / i1)) { | ||
| // handle error | ||
| return; // handle error | ||
| } | ||
| } | ||
| } else { | ||
| if (i2 > 0) { | ||
| if (i1 < (INT_MIN / i2)) { | ||
| // handle error | ||
| return; // handle error | ||
| } | ||
| } else { | ||
| if ((i1 != 0) && (i2 < (INT_MAX / i1))) { | ||
| // handle error | ||
| return; // handle error | ||
| } | ||
| } | ||
| } | ||
| i1 *i2; // COMPLIANT | ||
| i1 *= i2; // COMPLIANT | ||
| } | ||
|
|
||
| void test_simple_div(signed int i1, signed int i2) { | ||
| i1 / i2; // NON_COMPLIANT | ||
| i1 /= i2; // NON_COMPLIANT | ||
| } | ||
|
|
||
| void test_simple_div_no_zero(signed int i1, signed int i2) { | ||
| if (i2 == 0) { | ||
| // handle error | ||
| } else { | ||
| i1 / i2; // NON_COMPLIANT | ||
| i1 /= i2; // NON_COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_div_precheck(signed int i1, signed int i2) { | ||
| if ((i2 == 0) || ((i1 == INT_MIN) && (i2 == -1))) { | ||
| /* Handle error */ | ||
| } else { | ||
| i1 / i2; // COMPLIANT | ||
| i1 /= i2; // COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_simple_rem(signed int i1, signed int i2) { | ||
| i1 % i2; // NON_COMPLIANT | ||
| i1 %= i2; // NON_COMPLIANT | ||
| } | ||
|
|
||
| void test_simple_rem_no_zero(signed int i1, signed int i2) { | ||
| if (i2 == 0) { | ||
| // handle error | ||
| } else { | ||
| i1 % i2; // NON_COMPLIANT | ||
| i1 %= i2; // NON_COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_rem_precheck(signed int i1, signed int i2) { | ||
| if ((i2 == 0) || ((i1 == INT_MIN) && (i2 == -1))) { | ||
| /* Handle error */ | ||
| } else { | ||
| i1 % i2; // COMPLIANT | ||
| i1 %= i2; // COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_simple_negate(signed int i1) { | ||
| -i1; // NON_COMPLIANT | ||
| } | ||
|
|
||
| void test_negate_precheck(signed int i1) { | ||
| if (i1 == INT_MIN) { | ||
| // handle error | ||
| } else { | ||
| -i1; // COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_inc(signed int i1) { | ||
| i1++; // NON_COMPLIANT | ||
| } | ||
|
|
||
| void test_inc_guard(signed int i1) { | ||
| if (i1 < INT_MAX) { | ||
| i1++; // COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_inc_loop_guard() { | ||
| for (signed int i1 = 0; i1 < 10; i1++) { // COMPLIANT | ||
| // ... | ||
| } | ||
| } | ||
|
|
||
| void test_dec(signed int i1) { | ||
| i1--; // NON_COMPLIANT | ||
| } | ||
|
|
||
| void test_dec_guard(signed int i1) { | ||
| if (i1 > INT_MIN) { | ||
| i1--; // COMPLIANT | ||
| } | ||
| } | ||
|
|
||
| void test_dec_loop_guard() { | ||
| for (signed int i1 = 10; i1 > 0; i1--) { // COMPLIANT | ||
| // ... | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * @id cpp/misra/signed-integer-overflow | ||
| * @name RULE-4-1-3: Signed integer overflow leads to critical unspecified behavior | ||
| * @description Signed integer overflow or underflow from arithmetic operations results in critical | ||
| * unspecified behavior. | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @kind problem | ||
| * @precision medium | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-4-1-3 | ||
| * correctness | ||
| * scope/system | ||
| * external/misra/enforcement/undecidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.signedintegeroverflowshared.SignedIntegerOverflowShared | ||
|
|
||
| module SignedIntegerOverflowConfig implements SignedIntegerOverflowSharedConfigSig { | ||
| Query getQuery() { result = UndefinedPackage::signedIntegerOverflowQuery() } | ||
| } | ||
|
|
||
| import SignedIntegerOverflowShared<SignedIntegerOverflowConfig> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| cpp/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.ql |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.