Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7c3832f

Browse files
authored
Merge branch 'main' into lcartey/rule-2-5-alt-uses
2 parentsdd07767 +1566129 commit7c3832f

24 files changed

+410
-57
lines changed
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
import cpp
2+
import codingstandards.cpp.Pointers
23
import codingstandards.cpp.UndefinedBehavior
34

45
/**
56
* Library for modeling undefined behavior.
67
*/
78
abstractclassCUndefinedBehaviorextendsUndefinedBehavior{}
89

10+
/**
11+
* A function which has the signature - but not the name - of a main function.
12+
*/
913
classC99MainFunctionextendsFunction{
1014
C99MainFunction(){
1115
this.getNumberOfParameters()=2and
12-
this.getType()instanceofIntTypeand
13-
this.getParameter(0).getType()instanceofIntTypeand
14-
this.getParameter(1).getType().(PointerType).getBaseType().(PointerType).getBaseType()
15-
instanceofCharType
16+
this.getType().getUnderlyingType()instanceofIntTypeand
17+
this.getParameter(0).getType().getUnderlyingType()instanceofIntTypeand
18+
this.getParameter(1)
19+
.getType()
20+
.getUnderlyingType()
21+
.(UnspecifiedPointerOrArrayType)
22+
.getBaseType()
23+
.(UnspecifiedPointerOrArrayType)
24+
.getBaseType()instanceofCharType
1625
or
1726
this.getNumberOfParameters()=0and
18-
this.getType()instanceofVoidType
27+
// Must be explicitly declared as `int main(void)`.
28+
this.getADeclarationEntry().hasVoidParamList()and
29+
this.getType().getUnderlyingType()instanceofIntType
1930
}
2031
}
2132

2233
classCUndefinedMainDefinitionextendsCUndefinedBehavior,Function{
2334
CUndefinedMainDefinition(){
2435
// for testing purposes, we use the prefix ____codeql_coding_standards`
25-
(this.getName()="main"orthis.getName().indexOf("____codeql_coding_standards")=0)and
36+
(this.getName()="main"orthis.getName().indexOf("____codeql_coding_standards_main")=0)and
2637
notthisinstanceofC99MainFunction
2738
}
2839

2940
overridestringgetReason(){
3041
result=
31-
"The behavior of the program isundefined becausethe main functionis notdefined according to the C standard."
42+
"main function may triggerundefinedbehaviorbecauseitis notin one of the formats specified by the C standard."
3243
}
3344
}

‎c/misra/src/rules/RULE-1-3/OccurrenceOfUndefinedBehavior.ql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ import codingstandards.c.UndefinedBehavior
1818

1919
fromCUndefinedBehaviorc
2020
wherenotisExcluded(c, Language3Package::occurrenceOfUndefinedBehaviorQuery())
21-
selectc,"May result in undefined behavior."
21+
selectc,c.getReason()

‎c/misra/src/rules/RULE-5-4/MacroIdentifiersNotDistinct.ql‎

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,56 @@
1515

1616
import cpp
1717
import codingstandards.c.misra
18+
import codingstandards.cpp.Macro
19+
import codingstandards.cpp.Includes
20+
import codingstandards.cpp.PreprocessorDirective
1821

19-
fromMacrom,Macrom2
22+
/**
23+
* Gets a top level element that this macro is expanded to, e.g. an element which does not also have
24+
* an enclosing element in the macro.
25+
*/
26+
ElementgetATopLevelElement(MacroInvocationmi){
27+
result=mi.getAnExpandedElement()and
28+
notresult.getEnclosingElement()=mi.getAnExpandedElement()and
29+
notresultinstanceofConversion
30+
}
31+
32+
/**
33+
* Gets a link target that this macro is expanded in.
34+
*/
35+
LinkTargetgetALinkTarget(Macrom){
36+
exists(MacroInvocationmi,Elemente|
37+
mi=m.getAnInvocation()and
38+
e=getATopLevelElement(mi)
39+
|
40+
result=e.(Expr).getEnclosingFunction().getALinkTarget()
41+
or
42+
result=e.(Stmt).getEnclosingFunction().getALinkTarget()
43+
or
44+
exists(GlobalOrNamespaceVariableg|
45+
result=g.getALinkTarget()and
46+
g=e.(Expr).getEnclosingDeclaration()
47+
)
48+
)
49+
}
50+
51+
/**
52+
* Holds if the m1 and m2 are unconditionally included from a common file.
53+
*
54+
* Extracted out for performance reasons - otherwise the call to determine the file path for the
55+
* message was specializing the calls to `getAnUnconditionallyIncludedFile*(..)` and causing
56+
* slow performance.
57+
*/
58+
bindingset[m1, m2]
59+
pragma[inline_late]
60+
privatepredicateisIncludedUnconditionallyFromCommonFile(Macrom1,Macrom2){
61+
exists(Filef|
62+
getAnUnconditionallyIncludedFile*(f)=m1.getFile()and
63+
getAnUnconditionallyIncludedFile*(f)=m2.getFile()
64+
)
65+
}
66+
67+
fromMacrom,Macrom2,stringmessage
2068
where
2169
notisExcluded(m, Declarations1Package::macroIdentifiersNotDistinctQuery())and
2270
notm=m2and
@@ -25,12 +73,40 @@ where
2573
//C90 states the first 31 characters of macro identifiers are significant and is not currently considered by this rule
2674
//ie an identifier differing on the 32nd character would be indistinct for C90 but distinct for C99
2775
//and is currently not reported by this rule
28-
ifm.getName().length()>=64
29-
thenm.getName().prefix(63)=m2.getName().prefix(63)
30-
elsem.getName()=m2.getName()
76+
ifm.getName().length()>=64andnotm.getName()=m2.getName()
77+
then(
78+
m.getName().prefix(63)=m2.getName().prefix(63)and
79+
message=
80+
"Macro identifer "+m.getName()+" is nondistinct in first 63 characters, compared to $@."
81+
)else(
82+
m.getName()=m2.getName()and
83+
message=
84+
"Definition of macro "+m.getName()+
85+
" is not distinct from alternative definition of $@ in "+
86+
m2.getLocation().getFile().getRelativePath()+"."
87+
)
3188
)and
3289
//reduce double report since both macros are in alert, arbitrary ordering
33-
m.getLocation().getStartLine()>=m2.getLocation().getStartLine()
34-
selectm,
35-
"Macro identifer "+m.getName()+" is nondistinct in first 63 characters, compared to $@.",m2,
36-
m2.getName()
90+
m.getLocation().getStartLine()>=m2.getLocation().getStartLine()and
91+
// Not within an #ifndef MACRO_NAME
92+
notexists(PreprocessorIfndefifBranch|
93+
m.getAGuard()=ifBranchor
94+
m2.getAGuard()=ifBranch
95+
|
96+
ifBranch.getHead()=m.getName()
97+
)and
98+
// Must be included unconditionally from the same file, otherwise m1 may not be defined
99+
// when m2 is defined
100+
isIncludedUnconditionallyFromCommonFile(m,m2)and
101+
// Macros can't be mutually exclusive
102+
notmutuallyExclusiveBranchDirectiveMacros(m,m2)and
103+
notmutuallyExclusiveBranchDirectiveMacros(m2,m)and
104+
// If at least one invocation exists for at least one of the macros, then they must share a link
105+
// target - i.e. must both be expanded in the same context
106+
(
107+
(exists(m.getAnInvocation())andexists(m2.getAnInvocation()))
108+
implies
109+
// Must share a link target - e.g. must both be expanded in the same context
110+
getALinkTarget(m)=getALinkTarget(m2)
111+
)
112+
selectm,message,m2,m2.getName()

‎c/misra/src/rules/RULE-8-13/PointerShouldPointToConstTypeWhenPossible.ql‎

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,54 @@ import cpp
1818
import codingstandards.c.misra
1919
import codingstandards.cpp.Pointers
2020
import codingstandards.cpp.SideEffect
21+
import codingstandards.cpp.alertreporting.HoldsForAllCopies
2122

22-
fromVariableptr,PointerOrArrayTypetype
23+
classNonConstPointerVariableCandidateextendsVariable{
24+
NonConstPointerVariableCandidate(){
25+
// Ignore parameters in functions without bodies
26+
(thisinstanceofParameterimpliesexists(this.(Parameter).getFunction().getBlock()))and
27+
// Ignore variables in functions that use ASM commands
28+
notexists(AsmStmta|
29+
a.getEnclosingFunction()=this.(LocalScopeVariable).getFunction()
30+
or
31+
// In a type declared locally
32+
this.(Field).getDeclaringType+().getEnclosingFunction()=a.getEnclosingFunction()
33+
)and
34+
exists(PointerOrArrayTypetype|
35+
// include only pointers which point to a const-qualified type
36+
this.getType()=typeand
37+
nottype.isDeeplyConstBelow()
38+
)and
39+
// exclude pointers passed as arguments to functions which take a
40+
// parameter that points to a non-const-qualified type
41+
notexists(FunctionCallfc,inti|
42+
fc.getArgument(i)=this.getAnAccess()and
43+
notfc.getTarget().getParameter(i).getType().isDeeplyConstBelow()
44+
)and
45+
// exclude any pointers which have their underlying data modified
46+
notexists(VariableEffecteffect|
47+
effect.getTarget()=thisand
48+
// but not pointers that are only themselves modified
49+
noteffect.(AssignExpr).getLValue()=this.getAnAccess()and
50+
noteffect.(CrementOperation).getOperand()=this.getAnAccess()
51+
)and
52+
// exclude pointers assigned to another pointer to a non-const-qualified type
53+
notexists(Variablea|
54+
a.getAnAssignedValue()=this.getAnAccess()and
55+
nota.getType().(PointerOrArrayType).isDeeplyConstBelow()
56+
)
57+
}
58+
}
59+
60+
/**
61+
* Ensure that all copies of a variable are considered to be missing const qualification to avoid
62+
* false positives where a variable is only used/modified in a single copy.
63+
*/
64+
classNonConstPointerVariable=
65+
HoldsForAllCopies<NonConstPointerVariableCandidate,Variable>::LogicalResultElement;
66+
67+
fromNonConstPointerVariableptr
2368
where
24-
notisExcluded(ptr, Pointers1Package::pointerShouldPointToConstTypeWhenPossibleQuery())and
25-
// include only pointers which point to a const-qualified type
26-
ptr.getType()=typeand
27-
nottype.isDeeplyConstBelow()and
28-
// exclude pointers passed as arguments to functions which take a
29-
// parameter that points to a non-const-qualified type
30-
notexists(FunctionCallfc,inti|
31-
fc.getArgument(i)=ptr.getAnAccess()and
32-
notfc.getTarget().getParameter(i).getType().isDeeplyConstBelow()
33-
)and
34-
// exclude any pointers which have their underlying data modified
35-
notexists(VariableEffecteffect|
36-
effect.getTarget()=ptrand
37-
// but not pointers that are only themselves modified
38-
noteffect.(AssignExpr).getLValue()=effect.getAnAccess()and
39-
noteffect.(CrementOperation).getOperand()=effect.getAnAccess()
40-
)and
41-
// exclude pointers assigned to another pointer to a non-const-qualified type
42-
notexists(Variablea|
43-
a.getAnAssignedValue()=ptr.getAnAccess()and
44-
nota.getType().(PointerOrArrayType).isDeeplyConstBelow()
45-
)
46-
selectptr,"$@ points to a non-const-qualified type.",ptr,ptr.getName()
69+
notisExcluded(ptr.getAnElementInstance(),
70+
Pointers1Package::pointerShouldPointToConstTypeWhenPossibleQuery())
71+
selectptr,"$@ points to a non-const-qualified type.",ptr,ptr.getAnElementInstance().getName()
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
| test.c:8:6:8:35 | ____codeql_coding_standards_m2 | May result in undefined behavior. |
2-
| test.c:11:5:11:34 | ____codeql_coding_standards_m3 | May result in undefined behavior. |
3-
| test.c:15:5:15:34 | ____codeql_coding_standards_m4 | May result in undefined behavior. |
4-
| test.c:19:5:19:34 | ____codeql_coding_standards_m5 | May result in undefined behavior. |
5-
| test.c:23:5:23:34 | ____codeql_coding_standards_m6 | May result in undefined behavior. |
1+
| test.c:4:6:4:38 | ____codeql_coding_standards_main1 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |
2+
| test.c:8:5:8:37 | ____codeql_coding_standards_main2 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |
3+
| test.c:27:5:27:37 | ____codeql_coding_standards_main6 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |
4+
| test.c:32:6:32:38 | ____codeql_coding_standards_main7 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |
5+
| test.c:36:5:36:37 | ____codeql_coding_standards_main8 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |
6+
| test.c:40:5:40:37 | ____codeql_coding_standards_main9 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |
7+
| test.c:44:5:44:38 | ____codeql_coding_standards_main10 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |
8+
| test.c:48:5:48:38 | ____codeql_coding_standards_main11 | main function may trigger undefined behavior because it is not in one of the formats specified by the C standard. |

‎c/misra/test/rules/RULE-1-3/test.c‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
1-
voidmain(void) {// COMPLIANT
1+
intmain(void) {// COMPLIANT
22
}
33

4-
int____codeql_coding_standards_m1(intargc,char**argv) {// NON_COMPLIANT
4+
void____codeql_coding_standards_main1(void) {// NON_COMPLIANT
55
return0;
66
}
77

8-
void____codeql_coding_standards_m2(char*argc,char**argv) {// NON_COMPLIANT
8+
int____codeql_coding_standards_main2() {// NON_COMPLIANT
9+
return0;
10+
}
11+
12+
int____codeql_coding_standards_main3(intargc,char**argv) {// COMPLIANT
13+
return0;
14+
}
15+
16+
int____codeql_coding_standards_main4(intargc,charargv[][]) {// COMPLIANT
17+
return0;
18+
}
19+
20+
int____codeql_coding_standards_main5(intargc,char*argv[]) {// COMPLIANT
21+
return0;
22+
}
23+
24+
typedefintMY_INT;
25+
typedefchar*MY_CHAR_PTR;
26+
27+
int____codeql_coding_standards_main6(MY_INTargc,
28+
MY_CHAR_PTRargv[]) {// COMPLIANT
29+
return0;
30+
}
31+
32+
void____codeql_coding_standards_main7(char*argc,
33+
char**argv) {// NON_COMPLIANT
934
}
1035

11-
int____codeql_coding_standards_m3(intargc,char*argv) {// NON_COMPLIANT
36+
int____codeql_coding_standards_main8(intargc,char*argv) {// NON_COMPLIANT
1237
return0;
1338
}
1439

15-
int____codeql_coding_standards_m4() {// NON_COMPLIANT
40+
int____codeql_coding_standards_main9() {// NON_COMPLIANT
1641
return0;
1742
}
1843

19-
int____codeql_coding_standards_m5(intargc,int*argv) {// NON_COMPLIANT
44+
int____codeql_coding_standards_main10(intargc,int*argv) {// NON_COMPLIANT
2045
return0;
2146
}
2247

23-
int____codeql_coding_standards_m6(intargc,int**argv) {// NON_COMPLIANT
48+
int____codeql_coding_standards_main11(intargc,int**argv) {// NON_COMPLIANT
2449
return0;
2550
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
| header3.h:7:1:7:24 | #define MULTIPLE_INCLUDE | Definition of macro MULTIPLE_INCLUDE is not distinct from alternative definition of $@ in rules/RULE-5-4/header4.h. | header4.h:1:1:1:24 | #define MULTIPLE_INCLUDE | MULTIPLE_INCLUDE |
2+
| header3.h:14:1:14:21 | #define NOT_PROTECTED | Definition of macro NOT_PROTECTED is not distinct from alternative definition of $@ in rules/RULE-5-4/header4.h. | header4.h:12:1:12:23 | #define NOT_PROTECTED 1 | NOT_PROTECTED |
13
| test.c:2:1:2:72 | #define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB | Macro identifer iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB is nondistinct in first 63 characters, compared to $@. | test.c:1:1:1:72 | #define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA |
2-
| test.c:8:1:8:31 | #define FUNCTION_MACRO(X) X + 1 |Macro identiferFUNCTION_MACRO isnondistinct in first 63 characters, compared to $@. | test.c:7:1:7:57 | #define FUNCTION_MACRO(FUNCTION_MACRO) FUNCTION_MACRO + 1 | FUNCTION_MACRO |
4+
| test.c:8:1:8:31 | #define FUNCTION_MACRO(X) X + 1 |Definition of macroFUNCTION_MACRO isnot distinct from alternative definition of $@ in rules/RULE-5-4/test.c. | test.c:7:1:7:57 | #define FUNCTION_MACRO(FUNCTION_MACRO) FUNCTION_MACRO + 1 | FUNCTION_MACRO |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifdefFOO
2+
#include"header1.h"
3+
#else
4+
#include"header2.h"
5+
#endif
6+
7+
#ifdefFOO
8+
#defineA_MACRO 1 // COMPLIANT
9+
#else
10+
#defineA_MACRO 2 // COMPLIANT
11+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#defineREPEATED 11 // COMPLIANT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#defineREPEATED 1 // COMPLIANT

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp