Skip to content

Commit a20839c

Browse files
authored
Merge branch 'main' into lcartey/rule-10-3-bitwise
2 parents 3fd82fd + 1544a28 commit a20839c

16 files changed

+107
-56
lines changed

c/cert/src/rules/STR34-C/CastCharBeforeConvertingToLargerSizes.ql

+7-14
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,11 @@
1414

1515
import cpp
1616
import codingstandards.c.cert
17-
import semmle.code.cpp.commons.CommonType
17+
import codingstandards.cpp.rules.castcharbeforeconvertingtolargersizes.CastCharBeforeConvertingToLargerSizes
1818

19-
from Cast c
20-
where
21-
not isExcluded(c, Strings3Package::castCharBeforeConvertingToLargerSizesQuery()) and
22-
// find cases where there is a conversion happening wherein the
23-
// base type is a char
24-
c.getExpr().getType() instanceof CharType and
25-
not c.getExpr().getType() instanceof UnsignedCharType and
26-
// it's a bigger type
27-
c.getType().getSize() > c.getExpr().getType().getSize() and
28-
// and it's some kind of integer type
29-
c.getType() instanceof IntegralType
30-
select c.getExpr(),
31-
"Expression not converted to `unsigned char` before converting to a larger integer type."
19+
class CastCharBeforeConvertingToLargerSizesQuery extends CastCharBeforeConvertingToLargerSizesSharedQuery
20+
{
21+
CastCharBeforeConvertingToLargerSizesQuery() {
22+
this = Strings3Package::castCharBeforeConvertingToLargerSizesQuery()
23+
}
24+
}

c/cert/test/rules/STR34-C/CastCharBeforeConvertingToLargerSizes.expected

-21
This file was deleted.

c/cert/test/rules/STR34-C/CastCharBeforeConvertingToLargerSizes.qlref

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/castcharbeforeconvertingtolargersizes/CastCharBeforeConvertingToLargerSizes.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
| test.c:9:7:9:14 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2+
| test.c:30:11:30:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
3+
| test.c:31:3:31:13 | (unsigned int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
4+
| test.c:31:11:31:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
5+
| test.c:33:11:33:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
6+
| test.c:34:11:34:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
7+
| test.c:35:3:35:13 | (unsigned int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
8+
| test.c:35:11:35:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
9+
| test.c:36:3:36:13 | (unsigned int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
10+
| test.c:36:11:36:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
11+
| test.c:37:3:37:13 | (unsigned int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
12+
| test.c:37:11:37:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
13+
| test.c:38:3:38:13 | (unsigned int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
14+
| test.c:38:11:38:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
15+
| test.c:39:11:39:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
16+
| test.c:40:11:40:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
17+
| test.c:41:3:41:13 | (unsigned int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
18+
| test.c:41:11:41:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
19+
| test.c:42:12:42:13 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
20+
| test.c:44:11:44:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
21+
| test.c:45:11:45:12 | (int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
| test.c:7:7:7:14 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2-
| test.c:28:3:28:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
3-
| test.c:29:3:29:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
1+
| test.c:9:7:9:14 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2+
| test.c:30:3:30:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
43
| test.c:31:3:31:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
5-
| test.c:32:3:32:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
64
| test.c:33:3:33:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
75
| test.c:34:3:34:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
86
| test.c:35:3:35:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
97
| test.c:36:3:36:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
108
| test.c:37:3:37:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
119
| test.c:38:3:38:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
1210
| test.c:39:3:39:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
13-
| test.c:40:3:40:14 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
14-
| test.c:42:11:42:12 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
15-
| test.c:43:11:43:12 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
11+
| test.c:40:3:40:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
12+
| test.c:41:3:41:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
13+
| test.c:42:3:42:14 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
14+
| test.c:44:11:44:12 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
15+
| test.c:45:11:45:12 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
| test.c:7:7:7:14 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2-
| test.c:28:3:28:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
3-
| test.c:29:3:29:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
1+
| test.c:9:7:9:14 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2+
| test.c:30:3:30:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
43
| test.c:31:3:31:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
5-
| test.c:32:3:32:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
64
| test.c:33:3:33:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
75
| test.c:34:3:34:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
86
| test.c:35:3:35:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
97
| test.c:36:3:36:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
108
| test.c:37:3:37:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
119
| test.c:38:3:38:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
1210
| test.c:39:3:39:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
13-
| test.c:40:3:40:14 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
14-
| test.c:42:11:42:12 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
11+
| test.c:40:3:40:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
12+
| test.c:41:3:41:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
13+
| test.c:42:3:42:14 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
1514
| test.c:43:11:43:12 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
15+
| test.c:44:11:44:12 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
| test.c:7:7:7:14 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2-
| test.c:28:3:28:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
3-
| test.c:29:3:29:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
1+
| test.c:9:7:9:14 | * ... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2+
| test.c:30:3:30:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
43
| test.c:31:3:31:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
5-
| test.c:32:3:32:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
64
| test.c:33:3:33:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
75
| test.c:34:3:34:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
86
| test.c:35:3:35:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
97
| test.c:36:3:36:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
108
| test.c:37:3:37:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
119
| test.c:38:3:38:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
1210
| test.c:39:3:39:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
13-
| test.c:40:3:40:14 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
14-
| test.c:42:3:42:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
15-
| test.c:43:3:43:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
11+
| test.c:40:3:40:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
12+
| test.c:41:3:41:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
13+
| test.c:42:3:42:14 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
14+
| test.c:44:3:44:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
15+
| test.c:45:3:45:13 | (...) | Expression not converted to `unsigned char` before converting to a larger integer type. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.castcharbeforeconvertingtolargersizes.CastCharBeforeConvertingToLargerSizes
3+
4+
class TestFileQuery extends CastCharBeforeConvertingToLargerSizesSharedQuery, TestQuery { }

c/cert/test/rules/STR34-C/test.c c/common/test/rules/castcharbeforeconvertingtolargersizes/test.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
2+
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
13
#include <ctype.h>
24
#include <stdio.h>
35

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `STR34-C` - `CastCharBeforeConvertingToLargerSizes.ql`:
2+
- Fixes #576. Do not consider integer type aliases in templates.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Provides a library which includes a `problems` predicate for reporting....
3+
*/
4+
5+
import cpp
6+
import codingstandards.cpp.Customizations
7+
import codingstandards.cpp.Exclusions
8+
9+
abstract class CastCharBeforeConvertingToLargerSizesSharedQuery extends Query { }
10+
11+
Query getQuery() { result instanceof CastCharBeforeConvertingToLargerSizesSharedQuery }
12+
13+
query predicate problems(Cast c, string message) {
14+
not isExcluded(c, getQuery()) and
15+
// find cases where there is a conversion happening wherein the
16+
// base type is a char
17+
c.getExpr().getType() instanceof CharType and
18+
not c.getExpr().getType() instanceof UnsignedCharType and
19+
// it's a bigger type
20+
c.getType().getSize() > c.getExpr().getType().getSize() and
21+
// and it's some kind of integer type
22+
c.getType().getUnderlyingType() instanceof IntegralType and
23+
not c.isFromTemplateInstantiation(_) and
24+
message =
25+
"Expression not converted to `unsigned char` before converting to a larger integer type."
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:11:9:11:9 | (int32_t)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
2+
| test.cpp:12:41:12:41 | (signed int)... | Expression not converted to `unsigned char` before converting to a larger integer type. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.castcharbeforeconvertingtolargersizes.CastCharBeforeConvertingToLargerSizes
3+
4+
class TestFileQuery extends CastCharBeforeConvertingToLargerSizesSharedQuery, TestQuery { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C TEST CASE AND
2+
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
3+
#include <cstdint>
4+
5+
template <typename S, typename T> S get(T t) {
6+
S s = t; // COMPLIANT
7+
return s;
8+
}
9+
10+
void test(std::int32_t i32, std::int8_t i8, char c) {
11+
i32 = c; // NON_COMPLIANT
12+
i32 = get<std::int32_t, std::int32_t>(c); // NON_COMPLIANT
13+
i32 = get<std::int32_t, std::int8_t>(c); // COMPLIANT
14+
i32 = i8; // COMPLIANT
15+
i32 = get<std::int32_t, std::int32_t>(i8); // COMPLIANT
16+
i32 = get<std::int32_t, std::int8_t>(i8); // COMPLIANT
17+
}

rule_packages/c/Strings3.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"precision": "very-high",
1313
"severity": "error",
1414
"short_name": "CastCharBeforeConvertingToLargerSizes",
15+
"shared_implementation_short_name": "CastCharBeforeConvertingToLargerSizes",
1516
"tags": [
1617
"correctness",
1718
"security"

0 commit comments

Comments
 (0)