Skip to content

Commit 9969544

Browse files
committed
Merge branch 'main' into lcartey/cpp-conversions2
2 parents 3477dcf + e5d6fe6 commit 9969544

File tree

80 files changed

+3060
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+3060
-226
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-`A3-9-1` - `VariableWidthIntegerTypesUsed.ql`:
2+
- This query now reports the use of non-fixed width integer types in function return types, with the exception of `char` types and for `main` functions.

‎cpp/autosar/src/codingstandards/cpp/CommonTypes.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cpp as default
22

33
/*
4-
* Implementations of the C/C++ Fixed Width Types from cstdint.h.
4+
* Implementations of the C/C++ Fixed Width Types from cstdint.
55
*
66
* TODO: Deprecate once this is available in the CodeQL standard library.
77
*/

‎cpp/autosar/src/rules/A3-9-1/VariableWidthIntegerTypesUsed.ql‎

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,10 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20-
import codingstandards.cpp.EncapsulatingFunctions
21-
import codingstandards.cpp.BuiltInNumericTypes
22-
import codingstandards.cpp.Type
23-
import codingstandards.cpp.Operator
20+
import codingstandards.cpp.rules.variablewidthintegertypesused.VariableWidthIntegerTypesUsed
2421

25-
fromVariablev,TypetypeStrippedOfSpecifiers
26-
where
27-
notisExcluded(v, DeclarationsPackage::variableWidthIntegerTypesUsedQuery())and
28-
typeStrippedOfSpecifiers=stripSpecifiers(v.getType())and
29-
(
30-
typeStrippedOfSpecifiersinstanceofBuiltInIntegerTypeor
31-
typeStrippedOfSpecifiersinstanceofUnsignedCharTypeor
32-
typeStrippedOfSpecifiersinstanceofSignedCharType
33-
)and
34-
notvinstanceofExcludedVariableand
35-
// Dont consider template instantiations because instantiations with
36-
// Fixed Width Types are recorded after stripping their typedef'd type,
37-
// thereby, causing false positives (#540).
38-
notv.isFromTemplateInstantiation(_)and
39-
//post-increment/post-decrement operators are required by the standard to have a dummy int parameter
40-
notv.(Parameter).getFunction()instanceofPostIncrementOperatorand
41-
notv.(Parameter).getFunction()instanceofPostDecrementOperator
42-
selectv,"Variable '"+v.getName()+"' has variable-width type."
22+
classVariableWidthIntegerTypesUsedQueryextendsVariableWidthIntegerTypesUsedSharedQuery{
23+
VariableWidthIntegerTypesUsedQuery(){
24+
this= DeclarationsPackage::variableWidthIntegerTypesUsedQuery()
25+
}
26+
}

‎cpp/autosar/test/rules/A3-9-1/VariableWidthIntegerTypesUsed.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+
cpp/common/test/rules/variablewidthintegertypesused/VariableWidthIntegerTypesUsed.ql
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width char type. |
2-
| test.cpp:38:14:38:15 | c1 | Variable 'c1' has variable-width char type. |
3-
| test.cpp:56:17:56:18 | c2 | Variable 'c2' has variable-width char type. |
2+
| test.cpp:10:14:10:15 | c1 | Variable 'c1' has variable-width char type. |
3+
| test.cpp:14:17:14:18 | c2 | Variable 'c2' has variable-width char type. |

‎cpp/autosar/test/rules/A3-9-1/test.cpp‎

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,16 @@
22

33
voidtest_variable_width_type_variables(){
44
char c; // NON_COMPLIANT
5-
unsignedchar uc; // NON_COMPLIANT
6-
signedchar sc; // NON_COMPLIANT
7-
8-
int i; // NON_COMPLIANT
9-
unsignedint ui; // NON_COMPLIANT
10-
unsigned u; // NON_COMPLIANT
11-
signedint si; // NON_COMPLIANT
12-
signed s; // NON_COMPLIANT
13-
14-
short sh; // NON_COMPLIANT
15-
unsignedshort ush; // NON_COMPLIANT
16-
signedshort ssh; // NON_COMPLIANT
17-
18-
long l; // NON_COMPLIANT
19-
unsignedlong ul; // NON_COMPLIANT
20-
signedlong sl; // NON_COMPLIANT
21-
22-
std::int8_ti8; // COMPLIANT
23-
std::int16_ti16; // COMPLIANT
24-
std::int32_ti32; // COMPLIANT
25-
std::int64_ti64; // COMPLIANT
26-
27-
std::uint8_tu8; // COMPLIANT
28-
std::uint16_tu16; // COMPLIANT
29-
std::uint32_tu32; // COMPLIANT
30-
std::uint64_tu64; // COMPLIANT
31-
}
32-
33-
intmain(int argc, char *argv[]){// COMPLIANT
34-
// main as an exception
5+
unsignedchar uc; // COMPLIANT - covered by VariableWidthIntegerTypesUsed
6+
signedchar sc; // COMPLIANT - covered by VariableWidthIntegerTypesUsed
357
}
368

379
voidtest_variable_width_type_qualified_variables(){
3810
constchar c1 = 0; // NON_COMPLIANT
39-
constunsignedchar uc1 = 0; // NON_COMPLIANT
40-
constsignedchar sc1 = 0; // NON_COMPLIANt
41-
42-
constint i1 = 0; // NON_COMPLIANT
43-
constunsignedint ui1 = 0; // NON_COMPLIANT
44-
constunsigned u1 = 0; // NON_COMPLIANT
45-
constsignedint si1 = 0; // NON_COMPLIANT
46-
constsigned s1 = 0; // NON_COMPLIANT
47-
48-
constshort sh1 = 0; // NON_COMPLIANT
49-
constunsignedshort ush1 = 0; // NON_COMPLIANT
50-
constsignedshort ssh1 = 0; // NON_COMPLIANT
51-
52-
constlong l1 = 0; // NON_COMPLIANT
53-
constunsignedlong ul1 = 0; // NON_COMPLIANT
54-
constsignedlong sl1 = 0; // NON_COMPLIANT
11+
constunsignedchar uc1 = 0; // COMPLIANT - (VariableWidthIntegerTypesUsed)
12+
constsignedchar sc1 = 0; // COMPLIANT - (VariableWidthIntegerTypesUsed)
5513

5614
volatilechar c2; // NON_COMPLIANT
57-
volatileunsignedchar uc2; // NON_COMPLIANT
58-
volatilesignedchar sc2; // NON_COMPLIANt
59-
60-
volatileint i2; // NON_COMPLIANT
61-
volatileunsignedint ui2; // NON_COMPLIANT
62-
volatileunsigned u2; // NON_COMPLIANT
63-
volatilesignedint si2; // NON_COMPLIANT
64-
volatilesigned s2; // NON_COMPLIANT
65-
66-
volatileshort sh2; // NON_COMPLIANT
67-
volatileunsignedshort ush2; // NON_COMPLIANT
68-
volatilesignedshort ssh2; // NON_COMPLIANT
69-
70-
volatilelong l2; // NON_COMPLIANT
71-
volatileunsignedlong ul2; // NON_COMPLIANT
72-
volatilesignedlong sl2; // NON_COMPLIANT
73-
}
74-
75-
structtest_fix_fp_614{
76-
test_fix_fp_614 operator++(int); // COMPLIANT
77-
test_fix_fp_614 operator--(int); // COMPLIANT
78-
};
79-
80-
// COMPLIANT - instantiated with Fixed Width Types.
81-
template <typename MyType> constexprvoidtest_fix_fp_540(MyType value){
82-
value++;
83-
}
84-
85-
intcall_test_fix_fp_540(){
86-
test_fix_fp_540<std::uint8_t>(19);
87-
test_fix_fp_540<std::int16_t>(20);
88-
return0;
89-
}
15+
volatileunsignedchar uc2; // COMPLIANT - (VariableWidthIntegerTypesUsed)
16+
volatilesignedchar sc2; // COMPLIANT - (VariableWidthIntegerTypesUsed)
17+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* A library for supporting the consistent detection of banned functions in C++ code.
3+
*/
4+
5+
import cpp
6+
import AlertReporting
7+
8+
/**
9+
* A signature for a banned function.
10+
*/
11+
signatureclassBannedFunctionextendsFunction;
12+
13+
/**
14+
* A module for detecting uses of banned functions in C++ code.
15+
*/
16+
module BannedFunctions<BannedFunction F>{
17+
finalprivateclassFinalExpr=Expr;
18+
19+
/**
20+
* An expression that uses a banned function.
21+
*
22+
* It can be either a function call or a function access (taking the address of the function).
23+
*/
24+
classUseExprextendsFinalExpr{
25+
stringaction;
26+
FbannedFunction;
27+
28+
UseExpr(){
29+
this.(FunctionCall).getTarget()=bannedFunctionand
30+
action="Call to"
31+
or
32+
this.(FunctionAccess).getTarget()=bannedFunctionand
33+
action="Address taken for"
34+
}
35+
36+
stringgetFunctionName(){result=bannedFunction.getName()}
37+
38+
stringgetAction(){result=action}
39+
40+
ElementgetPrimaryElement(){
41+
// If this is defined in a macro in the users source location, then report the macro
42+
// expansion, otherwise report the element itself. This ensures that we always report
43+
// the use of the terminating function, but combine usages when the macro is defined
44+
// by the user.
45+
exists(Elemente|e= MacroUnwrapper<UseExpr>::unwrapElement(this)|
46+
ifexists(e.getFile().getRelativePath())thenresult=eelseresult=this
47+
)
48+
}
49+
}
50+
51+
finalprivateclassFinalElement=Element;
52+
53+
/**
54+
* A `Use` of a banned function.
55+
*
56+
* This is an `Element` in a program which represents the use of a banned function.
57+
* For uses within macro expansions, this may report the location of the macro, if
58+
* it is defined within the user's source code.
59+
*/
60+
classUseextendsFinalElement{
61+
UseExpruse;
62+
63+
Use(){this=use.getPrimaryElement()}
64+
65+
stringgetFunctionName(){result=use.getFunctionName()}
66+
67+
stringgetAction(){result=use.getAction()}
68+
}
69+
}

‎cpp/common/src/codingstandards/cpp/BuiltInNumericTypes.qll‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ class BuiltInIntegerType extends BuiltInType{
2020
classExcludedVariableextendsParameter{
2121
ExcludedVariable(){getFunction()instanceofMainFunction}
2222
}
23+
24+
/**
25+
* Any main function.
26+
*/
27+
classExcludedFunctionextendsFunction{
28+
ExcludedFunction(){thisinstanceofMainFunction}
29+
}

0 commit comments

Comments
(0)