Skip to content

Commit f9070ca

Browse files
authored
Merge pull request #752 from github/lcartey/improve-integer-suffix-testing
Expand integer constant testing, and extend C++ queries to support binary literals
2 parents d201f52 + 8760c3c commit f9070ca

File tree

6 files changed

+663
-5
lines changed

6 files changed

+663
-5
lines changed

c/misra/test/rules/RULE-7-2/UOrUSuffixRepresentedInUnsignedType.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@
44
| test.c:162:3:162:21 | 9223372036854775808 | Unsigned literal 0x8000000000000000L does not explicitly express sign with a 'U' or 'u' suffix. |
55
| test.c:185:3:185:22 | 9223372036854775808 | Unsigned literal 0x8000000000000000ll does not explicitly express sign with a 'U' or 'u' suffix. |
66
| test.c:208:3:208:22 | 9223372036854775808 | Unsigned literal 0x8000000000000000LL does not explicitly express sign with a 'U' or 'u' suffix. |
7+
| test.c:227:3:227:14 | 2147483648 | Unsigned literal 020000000000 does not explicitly express sign with a 'U' or 'u' suffix. |
8+
| test.c:232:3:232:25 | 9223372036854775808 | Unsigned literal 01000000000000000000000 does not explicitly express sign with a 'U' or 'u' suffix. |
9+
| test.c:249:3:249:26 | 9223372036854775808 | Unsigned literal 01000000000000000000000l does not explicitly express sign with a 'U' or 'u' suffix. |
10+
| test.c:266:3:266:26 | 9223372036854775808 | Unsigned literal 01000000000000000000000L does not explicitly express sign with a 'U' or 'u' suffix. |
11+
| test.c:283:3:283:27 | 9223372036854775808 | Unsigned literal 01000000000000000000000ll does not explicitly express sign with a 'U' or 'u' suffix. |
12+
| test.c:300:3:300:27 | 9223372036854775808 | Unsigned literal 01000000000000000000000LL does not explicitly express sign with a 'U' or 'u' suffix. |

c/misra/test/rules/RULE-7-2/test.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,94 @@ void test_hexadecimal_constants() {
221221
0x8000000000000000LLu; // COMPLIANT - unsigned, but uses the suffix correctly
222222
}
223223

224+
void test_octal_constants() {
225+
00; // COMPLIANT - uses signed int
226+
017777777777; // COMPLIANT - max value held by signed int
227+
020000000000; // NON_COMPLIANT - larger than max signed int, so will be
228+
// unsigned int
229+
040000000000; // COMPLIANT - larger than unsigned int, but smaller than long
230+
// int
231+
0777777777777777777777; // COMPLIANT - max long int
232+
01000000000000000000000; // NON_COMPLIANT - larger than long int, so will be
233+
// unsigned long int
234+
00U; // COMPLIANT - unsigned, but uses the suffix correctly
235+
017777777777U; // COMPLIANT - unsigned, but uses the suffix correctly
236+
020000000000U; // COMPLIANT - unsigned, but uses the suffix correctly
237+
040000000000U; // COMPLIANT - unsigned, but uses the suffix correctly
238+
0777777777777777777777U; // COMPLIANT - unsigned, but uses the suffix
239+
// correctly
240+
01000000000000000000000U; // COMPLIANT - unsigned, but uses the suffix
241+
// correctly
242+
243+
// Use of the `l` suffix
244+
00l; // COMPLIANT - uses signed long
245+
017777777777l; // COMPLIANT - uses signed long
246+
020000000000l; // COMPLIANT - uses signed long
247+
040000000000l; // COMPLIANT - uses signed long
248+
0777777777777777777777l; // COMPLIANT - max long int
249+
01000000000000000000000l; // NON_COMPLIANT - larger than long int, so will be
250+
// unsigned long int
251+
00Ul; // COMPLIANT - unsigned, but uses the suffix correctly
252+
017777777777Ul; // COMPLIANT - unsigned, but uses the suffix correctly
253+
020000000000Ul; // COMPLIANT - unsigned, but uses the suffix correctly
254+
040000000000Ul; // COMPLIANT - unsigned, but uses the suffix correctly
255+
0777777777777777777777Ul; // COMPLIANT - unsigned, but uses the suffix
256+
// correctly
257+
01000000000000000000000Ul; // COMPLIANT - unsigned, but uses the suffix
258+
// correctly
259+
260+
// Use of the `L` suffix
261+
00L; // COMPLIANT - uses signed long
262+
017777777777L; // COMPLIANT - uses signed long
263+
020000000000L; // COMPLIANT - uses signed long
264+
040000000000L; // COMPLIANT - uses signed long
265+
0777777777777777777777L; // COMPLIANT - COMPLIANT - uses signed long
266+
01000000000000000000000L; // NON_COMPLIANT - larger than long int, so will be
267+
// unsigned long int
268+
00UL; // COMPLIANT - unsigned, but uses the suffix correctly
269+
017777777777UL; // COMPLIANT - unsigned, but uses the suffix correctly
270+
020000000000UL; // COMPLIANT - unsigned, but uses the suffix correctly
271+
040000000000UL; // COMPLIANT - unsigned, but uses the suffix correctly
272+
0777777777777777777777UL; // COMPLIANT - unsigned, but uses the suffix
273+
// correctly
274+
01000000000000000000000UL; // COMPLIANT - unsigned, but uses the suffix
275+
// correctly
276+
277+
// Use of the `ll` suffix
278+
00ll; // COMPLIANT - uses signed long long
279+
017777777777ll; // COMPLIANT - uses signed long long
280+
020000000000ll; // COMPLIANT - uses signed long long
281+
040000000000ll; // COMPLIANT - uses signed long long
282+
0777777777777777777777ll; // COMPLIANT - max long int
283+
01000000000000000000000ll; // NON_COMPLIANT - larger than long int, so will be
284+
// unsigned long int
285+
00Ull; // COMPLIANT - unsigned, but uses the suffix correctly
286+
017777777777Ull; // COMPLIANT - unsigned, but uses the suffix correctly
287+
020000000000Ull; // COMPLIANT - unsigned, but uses the suffix correctly
288+
040000000000Ull; // COMPLIANT - unsigned, but uses the suffix correctly
289+
0777777777777777777777Ull; // COMPLIANT - unsigned, but uses the suffix
290+
// correctly
291+
01000000000000000000000Ull; // COMPLIANT - unsigned, but uses the suffix
292+
// correctly
293+
294+
// Use of the `LL` suffix
295+
00LL; // COMPLIANT - uses signed long long
296+
017777777777LL; // COMPLIANT - uses signed long long
297+
020000000000LL; // COMPLIANT - uses signed long long
298+
040000000000LL; // COMPLIANT - uses signed long long
299+
0777777777777777777777LL; // COMPLIANT - max long int
300+
01000000000000000000000LL; // NON_COMPLIANT - larger than long int, so will be
301+
// unsigned long int
302+
00ULL; // COMPLIANT - unsigned, but uses the suffix correctly
303+
017777777777ULL; // COMPLIANT - unsigned, but uses the suffix correctly
304+
020000000000ULL; // COMPLIANT - unsigned, but uses the suffix correctly
305+
040000000000ULL; // COMPLIANT - unsigned, but uses the suffix correctly
306+
0777777777777777777777ULL; // COMPLIANT - unsigned, but uses the suffix
307+
// correctly
308+
01000000000000000000000ULL; // COMPLIANT - unsigned, but uses the suffix
309+
// correctly
310+
}
311+
224312
#define COMPLIANT_VAL 0x80000000U
225313
#define NON_COMPLIANT_VAL 0x80000000
226314

change_notes/2024-10-17-suffixes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `5.13.4` - `UnsignedLiteralsNotAppropriatelySuffixed.ql`:
2+
- Expand detection to binary literals.
3+
- `M2-13-3` - `MissingUSuffix.ql`:
4+
- Expand detection to binary literals.

cpp/common/src/codingstandards/cpp/rules/unsignedintegerliteralsnotappropriatelysuffixed/UnsignedIntegerLiteralsNotAppropriatelySuffixed.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ query predicate problems(Cpp14Literal::NumericLiteral nl, string message) {
1919
nl instanceof Cpp14Literal::OctalLiteral and literalKind = "Octal"
2020
or
2121
nl instanceof Cpp14Literal::HexLiteral and literalKind = "Hex"
22+
or
23+
nl instanceof Cpp14Literal::BinaryLiteral and literalKind = "Binary"
2224
) and
2325
// This either directly has an unsigned integer type, or it is converted to an unsigned integer type
2426
nl.getType().getUnspecifiedType().(IntegralType).isUnsigned() and
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
| test.cpp:3:3:3:12 | 4294967295 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
1+
| test.cpp:111:3:111:12 | 2147483648 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
2+
| test.cpp:116:3:116:20 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
3+
| test.cpp:139:3:139:21 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
4+
| test.cpp:162:3:162:21 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
5+
| test.cpp:185:3:185:22 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
6+
| test.cpp:208:3:208:22 | 9223372036854775808 | Hex literal is an unsigned integer but does not include a 'U' suffix. |
7+
| test.cpp:227:3:227:14 | 2147483648 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
8+
| test.cpp:232:3:232:25 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
9+
| test.cpp:249:3:249:26 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
10+
| test.cpp:266:3:266:26 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
11+
| test.cpp:283:3:283:27 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
12+
| test.cpp:300:3:300:27 | 9223372036854775808 | Octal literal is an unsigned integer but does not include a 'U' suffix. |
13+
| test.cpp:315:3:315:36 | 2147483648 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
14+
| test.cpp:322:3:322:68 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
15+
| test.cpp:365:3:365:69 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
16+
| test.cpp:412:3:412:69 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
17+
| test.cpp:457:3:457:70 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |
18+
| test.cpp:502:3:502:70 | 9223372036854775808 | Binary literal is an unsigned integer but does not include a 'U' suffix. |

0 commit comments

Comments
 (0)