Skip to content

Expand integer constant testing, and extend C++ queries to support binary literals #752

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
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
| test.c:162:3:162:21 | 9223372036854775808 | Unsigned literal 0x8000000000000000L does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:185:3:185:22 | 9223372036854775808 | Unsigned literal 0x8000000000000000ll does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:208:3:208:22 | 9223372036854775808 | Unsigned literal 0x8000000000000000LL does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:227:3:227:14 | 2147483648 | Unsigned literal 020000000000 does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:232:3:232:25 | 9223372036854775808 | Unsigned literal 01000000000000000000000 does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:249:3:249:26 | 9223372036854775808 | Unsigned literal 01000000000000000000000l does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:266:3:266:26 | 9223372036854775808 | Unsigned literal 01000000000000000000000L does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:283:3:283:27 | 9223372036854775808 | Unsigned literal 01000000000000000000000ll does not explicitly express sign with a 'U' or 'u' suffix. |
| test.c:300:3:300:27 | 9223372036854775808 | Unsigned literal 01000000000000000000000LL does not explicitly express sign with a 'U' or 'u' suffix. |
88 changes: 88 additions & 0 deletions c/misra/test/rules/RULE-7-2/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,94 @@ void test_hexadecimal_constants() {
0x8000000000000000LLu; // COMPLIANT - unsigned, but uses the suffix correctly
}

void test_octal_constants() {
00; // COMPLIANT - uses signed int
017777777777; // COMPLIANT - max value held by signed int
020000000000; // NON_COMPLIANT - larger than max signed int, so will be
// unsigned int
040000000000; // COMPLIANT - larger than unsigned int, but smaller than long
// int
0777777777777777777777; // COMPLIANT - max long int
01000000000000000000000; // NON_COMPLIANT - larger than long int, so will be
// unsigned long int
00U; // COMPLIANT - unsigned, but uses the suffix correctly
017777777777U; // COMPLIANT - unsigned, but uses the suffix correctly
020000000000U; // COMPLIANT - unsigned, but uses the suffix correctly
040000000000U; // COMPLIANT - unsigned, but uses the suffix correctly
0777777777777777777777U; // COMPLIANT - unsigned, but uses the suffix
// correctly
01000000000000000000000U; // COMPLIANT - unsigned, but uses the suffix
// correctly

// Use of the `l` suffix
00l; // COMPLIANT - uses signed long
017777777777l; // COMPLIANT - uses signed long
020000000000l; // COMPLIANT - uses signed long
040000000000l; // COMPLIANT - uses signed long
0777777777777777777777l; // COMPLIANT - max long int
01000000000000000000000l; // NON_COMPLIANT - larger than long int, so will be
// unsigned long int
00Ul; // COMPLIANT - unsigned, but uses the suffix correctly
017777777777Ul; // COMPLIANT - unsigned, but uses the suffix correctly
020000000000Ul; // COMPLIANT - unsigned, but uses the suffix correctly
040000000000Ul; // COMPLIANT - unsigned, but uses the suffix correctly
0777777777777777777777Ul; // COMPLIANT - unsigned, but uses the suffix
// correctly
01000000000000000000000Ul; // COMPLIANT - unsigned, but uses the suffix
// correctly

// Use of the `L` suffix
00L; // COMPLIANT - uses signed long
017777777777L; // COMPLIANT - uses signed long
020000000000L; // COMPLIANT - uses signed long
040000000000L; // COMPLIANT - uses signed long
0777777777777777777777L; // COMPLIANT - COMPLIANT - uses signed long
01000000000000000000000L; // NON_COMPLIANT - larger than long int, so will be
// unsigned long int
00UL; // COMPLIANT - unsigned, but uses the suffix correctly
017777777777UL; // COMPLIANT - unsigned, but uses the suffix correctly
020000000000UL; // COMPLIANT - unsigned, but uses the suffix correctly
040000000000UL; // COMPLIANT - unsigned, but uses the suffix correctly
0777777777777777777777UL; // COMPLIANT - unsigned, but uses the suffix
// correctly
01000000000000000000000UL; // COMPLIANT - unsigned, but uses the suffix
// correctly

// Use of the `ll` suffix
00ll; // COMPLIANT - uses signed long long
017777777777ll; // COMPLIANT - uses signed long long
020000000000ll; // COMPLIANT - uses signed long long
040000000000ll; // COMPLIANT - uses signed long long
0777777777777777777777ll; // COMPLIANT - max long int
01000000000000000000000ll; // NON_COMPLIANT - larger than long int, so will be
// unsigned long int
00Ull; // COMPLIANT - unsigned, but uses the suffix correctly
017777777777Ull; // COMPLIANT - unsigned, but uses the suffix correctly
020000000000Ull; // COMPLIANT - unsigned, but uses the suffix correctly
040000000000Ull; // COMPLIANT - unsigned, but uses the suffix correctly
0777777777777777777777Ull; // COMPLIANT - unsigned, but uses the suffix
// correctly
01000000000000000000000Ull; // COMPLIANT - unsigned, but uses the suffix
// correctly

// Use of the `LL` suffix
00LL; // COMPLIANT - uses signed long long
017777777777LL; // COMPLIANT - uses signed long long
020000000000LL; // COMPLIANT - uses signed long long
040000000000LL; // COMPLIANT - uses signed long long
0777777777777777777777LL; // COMPLIANT - max long int
01000000000000000000000LL; // NON_COMPLIANT - larger than long int, so will be
// unsigned long int
00ULL; // COMPLIANT - unsigned, but uses the suffix correctly
017777777777ULL; // COMPLIANT - unsigned, but uses the suffix correctly
020000000000ULL; // COMPLIANT - unsigned, but uses the suffix correctly
040000000000ULL; // COMPLIANT - unsigned, but uses the suffix correctly
0777777777777777777777ULL; // COMPLIANT - unsigned, but uses the suffix
// correctly
01000000000000000000000ULL; // COMPLIANT - unsigned, but uses the suffix
// correctly
}

#define COMPLIANT_VAL 0x80000000U
#define NON_COMPLIANT_VAL 0x80000000

Expand Down
4 changes: 4 additions & 0 deletions change_notes/2024-10-17-suffixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- `5.13.4` - `UnsignedLiteralsNotAppropriatelySuffixed.ql`:
- Expand detection to binary literals.
- `M2-13-3` - `MissingUSuffix.ql`:
- Expand detection to binary literals.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ query predicate problems(Cpp14Literal::NumericLiteral nl, string message) {
nl instanceof Cpp14Literal::OctalLiteral and literalKind = "Octal"
or
nl instanceof Cpp14Literal::HexLiteral and literalKind = "Hex"
or
nl instanceof Cpp14Literal::BinaryLiteral and literalKind = "Binary"
) and
// This either directly has an unsigned integer type, or it is converted to an unsigned integer type
nl.getType().getUnspecifiedType().(IntegralType).isUnsigned() and
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
| test.cpp:3:3:3:12 | 4294967295 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:111:3:111:12 | 2147483648 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:116:3:116:20 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:139:3:139:21 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:162:3:162:21 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:185:3:185:22 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:208:3:208:22 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:227:3:227:14 | 2147483648 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:232:3:232:25 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:249:3:249:26 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:266:3:266:26 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:283:3:283:27 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:300:3:300:27 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:315:3:315:36 | 2147483648 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:322:3:322:68 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:365:3:365:69 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:412:3:412:69 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:457:3:457:70 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
| test.cpp:502:3:502:70 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
Loading
Loading