Skip to content

Commit bc00de6

Browse files
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/implement-package-generics
2 parents 2265ed4 + 6d7395a commit bc00de6

File tree

101 files changed

+1538
-360
lines changed

Some content is hidden

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

101 files changed

+1538
-360
lines changed

.github/workflows/upgrade_codeql_dependencies.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
find c \( -name '*.ql' -or -name '*.qll' \) -print0 | xargs -0 --max-procs "$XARGS_MAX_PROCS" codeql query format --in-place
5454
5555
- name: Create Pull Request
56-
uses: peter-evans/create-pull-request@67ccf781d68cd99b580ae25a5c18a1cc84ffff1f # v7.0.6
56+
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
5757
with:
5858
title: "Upgrade `github/codeql` dependency to ${{ github.event.inputs.codeql_cli_version }}"
5959
body: |

amendments.csv

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,40 @@ c,MISRA-C-2012,Amendment3,RULE-10-7,Yes,Refine,Yes,Import
1111
c,MISRA-C-2012,Amendment3,RULE-10-8,Yes,Refine,Yes,Import
1212
c,MISRA-C-2012,Amendment3,RULE-21-11,Yes,Clarification,Yes,Import
1313
c,MISRA-C-2012,Amendment3,RULE-21-12,Yes,Replace,No,Easy
14-
c,MISRA-C-2012,Amendment4,RULE-11-3,Yes,Expand,No,Easy
15-
c,MISRA-C-2012,Amendment4,RULE-11-8,Yes,Expand,No,Easy
16-
c,MISRA-C-2012,Amendment4,RULE-13-2,Yes,Expand,No,Very Hard
14+
c,MISRA-C-2012,Amendment4,RULE-11-3,Yes,Expand,Yes,Easy
15+
c,MISRA-C-2012,Amendment4,RULE-11-8,Yes,Expand,Yes,Easy
16+
c,MISRA-C-2012,Amendment4,RULE-13-2,Yes,Expand,Yes,Very Hard
1717
c,MISRA-C-2012,Amendment4,RULE-18-6,Yes,Expand,No,Medium
1818
c,MISRA-C-2012,Amendment4,RULE-18-8,Yes,Split,Yes,Easy
1919
c,MISRA-C-2012,Amendment4,RULE-2-2,Yes,Clarification,Yes,Import
2020
c,MISRA-C-2012,Amendment4,RULE-2-7,Yes,Clarification,Yes,Import
21-
c,MISRA-C-2012,Amendment4,RULE-3-1,Yes,Refine,No,Easy
21+
c,MISRA-C-2012,Amendment4,RULE-3-1,Yes,Refine,Yes,Easy
2222
c,MISRA-C-2012,Amendment4,RULE-8-6,Yes,Clarification,Yes,Import
2323
c,MISRA-C-2012,Amendment4,RULE-8-9,Yes,Clarification,Yes,Import
2424
c,MISRA-C-2012,Amendment4,RULE-9-4,Yes,Clarification,Yes,Import
2525
c,MISRA-C-2012,Amendment4,RULE-10-1,Yes,Clarification,Yes,Import
2626
c,MISRA-C-2012,Amendment4,RULE-18-3,Yes,Clarification,Yes,Import
2727
c,MISRA-C-2012,Amendment4,RULE-1-4,Yes,Replace,No,Easy
28-
c,MISRA-C-2012,Amendment4,RULE-9-1,Yes,Refine,No,Easy
28+
c,MISRA-C-2012,Amendment4,RULE-9-1,Yes,Refine,Yes,Easy
2929
c,MISRA-C-2012,Amendment4,RULE-9-2,Yes,Refine,No,Import
3030
c,MISRA-C-2012,Corrigendum2,DIR-4-10,Yes,Clarification,Yes,Import
31-
c,MISRA-C-2012,Corrigendum2,RULE-7-4,Yes,Refine,No,Easy
31+
c,MISRA-C-2012,Corrigendum2,RULE-7-4,Yes,Refine,Yes,Easy
3232
c,MISRA-C-2012,Corrigendum2,RULE-8-2,Yes,Clarification,Yes,Import
33-
c,MISRA-C-2012,Corrigendum2,RULE-8-3,Yes,Refine,No,Easy
33+
c,MISRA-C-2012,Corrigendum2,RULE-8-3,Yes,Refine,Yes,Easy
3434
c,MISRA-C-2012,Corrigendum2,RULE-8-7,Yes,Clarification,Yes,Import
3535
c,MISRA-C-2012,Corrigendum2,RULE-10-1,Yes,Clarification,Yes,Import
36-
c,MISRA-C-2012,Corrigendum2,RULE-10-2,Yes,Refine,No,Easy
36+
c,MISRA-C-2012,Corrigendum2,RULE-10-2,Yes,Refine,Yes,Easy
3737
c,MISRA-C-2012,Corrigendum2,RULE-10-3,Yes,Clarification,Yes,Import
3838
c,MISRA-C-2012,Corrigendum2,RULE-11-3,Yes,Clarification,Yes,Import
3939
c,MISRA-C-2012,Corrigendum2,RULE-11-6,Yes,Clarification,Yes,Import
4040
c,MISRA-C-2012,Corrigendum2,RULE-13-2,Yes,Clarification,Yes,Import
4141
c,MISRA-C-2012,Corrigendum2,RULE-13-6,Yes,Clarification,Yes,Import
42-
c,MISRA-C-2012,Corrigendum2,RULE-14-3,Yes,Refine,No,Easy
42+
c,MISRA-C-2012,Corrigendum2,RULE-14-3,Yes,Refine,Yes,Easy
4343
c,MISRA-C-2012,Corrigendum2,RULE-15-7,Yes,Clarification,Yes,Import
4444
c,MISRA-C-2012,Corrigendum2,RULE-17-4,Yes,Clarification,Yes,Import
4545
c,MISRA-C-2012,Corrigendum2,RULE-17-5,Yes,Clarification,Yes,Import
46-
c,MISRA-C-2012,Corrigendum2,RULE-18-1,Yes,Refine,No,Easy
46+
c,MISRA-C-2012,Corrigendum2,RULE-18-1,Yes,Refine,Yes,Easy
4747
c,MISRA-C-2012,Corrigendum2,RULE-20-14,No,Clarification,Yes,Import
4848
c,MISRA-C-2012,Corrigendum2,RULE-21-19,Yes,Clarification,Yes,Import
49-
c,MISRA-C-2012,Corrigendum2,RULE-21-20,Yes,Refine,No,Easy
50-
c,MISRA-C-2012,Corrigendum2,RULE-22-9,Yes,Clarification,Yes,Import
49+
c,MISRA-C-2012,Corrigendum2,RULE-21-20,Yes,Refine,Yes,Easy
50+
c,MISRA-C-2012,Corrigendum2,RULE-22-9,Yes,Clarification,Yes,Import

c/cert/src/qlpack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: codeql/cert-c-coding-standards
2-
version: 2.42.0-dev
2+
version: 2.43.0-dev
33
description: CERT C 2016
44
suites: codeql-suites
55
license: MIT

c/cert/test/qlpack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: codeql/cert-c-coding-standards-tests
2-
version: 2.42.0-dev
2+
version: 2.43.0-dev
33
extractor: cpp
44
license: MIT
55
dependencies:

c/common/src/qlpack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: codeql/common-c-coding-standards
2-
version: 2.42.0-dev
2+
version: 2.43.0-dev
33
license: MIT
44
dependencies:
55
codeql/common-cpp-coding-standards: '*'

c/common/test/qlpack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: codeql/common-c-coding-standards-tests
2-
version: 2.42.0-dev
2+
version: 2.43.0-dev
33
extractor: cpp
44
license: MIT
55
dependencies:
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| test.c:4:13:4:18 | ... + ... | Array pointer p2 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
2-
| test.c:5:13:5:18 | ... + ... | Array pointer p3 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
3-
| test.c:6:13:6:18 | & ... | Array pointer p4 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
4-
| test.c:11:8:11:11 | ... -- | Array pointer p7 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
5-
| test.c:12:8:12:9 | p3 | Array pointer p8 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
1+
| test.c:4:13:4:18 | ... + ... | Array pointer p2 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
2+
| test.c:5:13:5:18 | ... + ... | Array pointer p3 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
3+
| test.c:6:13:6:18 | & ... | Array pointer p4 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
4+
| test.c:11:8:11:11 | ... -- | Array pointer p7 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
5+
| test.c:12:8:12:9 | p3 | Array pointer p8 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |

c/common/test/rules/readofuninitializedmemory/test.c

+2
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ void test_non_default_init() {
9494
static struct A ss;
9595
use_struct_A(
9696
ss); // COMPLIANT - static struct type variables are zero initialized
97+
_Atomic int x;
98+
use_int(x); // COMPLIANT - atomics are special, covered by other rules
9799
}

c/misra/src/codingstandards/c/misra/EssentialTypes.qll

+18-9
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,14 @@ class EssentialBinaryOperationSubjectToUsualConversions extends EssentialExpr, B
328328
exists(
329329
Type leftEssentialType, Type rightEssentialType,
330330
EssentialTypeCategory leftEssentialTypeCategory,
331-
EssentialTypeCategory rightEssentialTypeCategory
331+
EssentialTypeCategory rightEssentialTypeCategory, int intTypeSize
332332
|
333333
leftEssentialType = getEssentialType(getLeftOperand()) and
334334
rightEssentialType = getEssentialType(getRightOperand()) and
335335
leftEssentialTypeCategory = getEssentialTypeCategory(leftEssentialType) and
336-
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType)
336+
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType) and
337+
// For rules around addition/subtraction with char types:
338+
intTypeSize = any(IntType i | i.isSigned()).getSize()
337339
|
338340
if
339341
leftEssentialTypeCategory = rightEssentialTypeCategory and
@@ -356,14 +358,18 @@ class EssentialBinaryOperationSubjectToUsualConversions extends EssentialExpr, B
356358
class EssentialAddExpr extends EssentialBinaryOperationSubjectToUsualConversions, AddExpr {
357359
override Type getEssentialType() {
358360
exists(
359-
EssentialTypeCategory operandTypeCategory, EssentialTypeCategory otherOperandTypeCategory
361+
Type otherOperandType, EssentialTypeCategory operandTypeCategory,
362+
EssentialTypeCategory otherOperandTypeCategory, int intTypeSize
360363
|
361364
operandTypeCategory = getEssentialTypeCategory(getEssentialType(getAnOperand())) and
362-
otherOperandTypeCategory = getEssentialTypeCategory(getEssentialType(getAnOperand()))
365+
otherOperandType = getEssentialType(getAnOperand()) and
366+
otherOperandTypeCategory = getEssentialTypeCategory(otherOperandType) and
367+
intTypeSize = any(IntType i).getSize()
363368
|
364369
if
365370
operandTypeCategory = EssentiallyCharacterType() and
366-
otherOperandTypeCategory instanceof EssentiallySignedOrUnsignedType
371+
otherOperandTypeCategory instanceof EssentiallySignedOrUnsignedType and
372+
otherOperandType.getSize() <= intTypeSize
367373
then result instanceof PlainCharType
368374
else result = super.getEssentialType()
369375
)
@@ -376,15 +382,18 @@ class EssentialAddExpr extends EssentialBinaryOperationSubjectToUsualConversions
376382
class EssentialSubExpr extends EssentialBinaryOperationSubjectToUsualConversions, SubExpr {
377383
override Type getEssentialType() {
378384
exists(
379-
EssentialTypeCategory leftEssentialTypeCategory,
380-
EssentialTypeCategory rightEssentialTypeCategory
385+
EssentialTypeCategory leftEssentialTypeCategory, Type rightEssentialType,
386+
EssentialTypeCategory rightEssentialTypeCategory, int intTypeSize
381387
|
382388
leftEssentialTypeCategory = getEssentialTypeCategory(getEssentialType(getLeftOperand())) and
383-
rightEssentialTypeCategory = getEssentialTypeCategory(getEssentialType(getRightOperand()))
389+
rightEssentialType = getEssentialType(getRightOperand()) and
390+
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType) and
391+
intTypeSize = any(IntType i).getSize()
384392
|
385393
if
386394
leftEssentialTypeCategory = EssentiallyCharacterType() and
387-
rightEssentialTypeCategory instanceof EssentiallySignedOrUnsignedType
395+
rightEssentialTypeCategory instanceof EssentiallySignedOrUnsignedType and
396+
rightEssentialType.getSize() <= intTypeSize
388397
then result instanceof PlainCharType
389398
else result = super.getEssentialType()
390399
)

c/misra/src/qlpack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: codeql/misra-c-coding-standards
2-
version: 2.42.0-dev
2+
version: 2.43.0-dev
33
description: MISRA C 2012
44
suites: codeql-suites
55
license: MIT

c/misra/src/rules/RULE-10-2/AdditionSubtractionOnEssentiallyCharType.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
// But the overall essential type is not essentially character type
3333
getEssentialTypeCategory(getEssentialType(addOrSub)) = EssentiallyCharacterType()
3434
or
35-
// Or this is a subtration of one character with another, which is permitted, but produces an integral type
35+
// Or this is a subtraction of one character with another, which is permitted, but produces an integral type
3636
getEssentialTypeCategory(getEssentialType(addOrSub.getLeftOperand())) =
3737
EssentiallyCharacterType() and
3838
getEssentialTypeCategory(getEssentialType(addOrSub.getRightOperand())) =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @id c/misra/atomic-qualifier-applied-to-void
3+
* @name RULE-11-10: The _Atomic qualifier shall not be applied to the incomplete type void
4+
* @description Conversions between types by using an _Atomic void type may result in undefined
5+
* behavior.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-11-10
10+
* correctness
11+
* external/misra/c/2012/third-edition-first-revision
12+
* external/misra/c/2012/amendment4
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
19+
class AtomicVoidType extends Type {
20+
AtomicVoidType() {
21+
hasSpecifier("atomic") and
22+
getUnspecifiedType() instanceof VoidType
23+
}
24+
}
25+
26+
predicate usesAtomicVoid(Type root) {
27+
root instanceof AtomicVoidType
28+
or
29+
usesAtomicVoid(root.(DerivedType).getBaseType())
30+
or
31+
usesAtomicVoid(root.(RoutineType).getReturnType())
32+
or
33+
usesAtomicVoid(root.(RoutineType).getAParameterType())
34+
or
35+
usesAtomicVoid(root.(FunctionPointerType).getReturnType())
36+
or
37+
usesAtomicVoid(root.(FunctionPointerType).getAParameterType())
38+
or
39+
usesAtomicVoid(root.(TypedefType).getBaseType())
40+
}
41+
42+
class ExplicitType extends Type {
43+
Element getDeclaration(string description) {
44+
result.(DeclarationEntry).getType() = this and description = result.(DeclarationEntry).getName()
45+
or
46+
result.(CStyleCast).getType() = this and description = "Cast"
47+
}
48+
}
49+
50+
from Element decl, ExplicitType explicitType, string elementDescription
51+
where
52+
not isExcluded(decl, Declarations9Package::atomicQualifierAppliedToVoidQuery()) and
53+
decl = explicitType.getDeclaration(elementDescription) and
54+
usesAtomicVoid(explicitType)
55+
select decl, elementDescription + " declared with an atomic void type."

c/misra/src/rules/RULE-11-3/CastBetweenObjectPointerAndDifferentObjectType.ql

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ where
2323
baseTypeFrom = cast.getExpr().getType().(PointerToObjectType).getBaseType() and
2424
baseTypeTo = cast.getType().(PointerToObjectType).getBaseType() and
2525
// exception: cast to a char, signed char, or unsigned char is permitted
26-
not baseTypeTo.stripType() instanceof CharType and
26+
not (
27+
baseTypeTo.stripType() instanceof CharType and
28+
// Exception does not apply to _Atomic types
29+
not baseTypeFrom.hasSpecifier("atomic")
30+
) and
2731
(
2832
(
2933
baseTypeFrom.isVolatile() and not baseTypeTo.isVolatile()

c/misra/src/rules/RULE-11-8/CastRemovesConstOrVolatileQualification.ql

+4
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ where
2424
baseTypeFrom.isVolatile() and not baseTypeTo.isVolatile() and qualificationName = "volatile"
2525
or
2626
baseTypeFrom.isConst() and not baseTypeTo.isConst() and qualificationName = "const"
27+
or
28+
baseTypeFrom.hasSpecifier("atomic") and
29+
not baseTypeTo.hasSpecifier("atomic") and
30+
qualificationName = "atomic"
2731
)
2832
select cast, "Cast of pointer removes " + qualificationName + " qualification from its base type."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @id c/misra/unsequenced-atomic-reads
3+
* @name RULE-13-2: The value of an atomic variable shall not depend on the evaluation order of interleaved threads
4+
* @description The value of an atomic variable shall not depend on evaluation order and
5+
* interleaving of threads.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-13-2
10+
* correctness
11+
* external/misra/c/2012/amendment3
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import semmle.code.cpp.dataflow.TaintTracking
17+
import codingstandards.c.misra
18+
import codingstandards.c.Ordering
19+
import codingstandards.c.orderofevaluation.VariableAccessOrdering
20+
21+
class AtomicAccessInFullExpressionOrdering extends Ordering::Configuration {
22+
AtomicAccessInFullExpressionOrdering() { this = "AtomicAccessInFullExpressionOrdering" }
23+
24+
override predicate isCandidate(Expr e1, Expr e2) {
25+
exists(AtomicVariableAccess a, AtomicVariableAccess b, FullExpr e | a = e1 and b = e2 |
26+
a.getTarget() = b.getTarget() and
27+
a.(ConstituentExpr).getFullExpr() = e and
28+
b.(ConstituentExpr).getFullExpr() = e and
29+
not a = b
30+
)
31+
}
32+
}
33+
34+
/**
35+
* A read of a variable specified as `_Atomic`.
36+
*
37+
* Note, it may be accessed directly, or by passing its address into the std atomic functions.
38+
*/
39+
class AtomicVariableAccess extends VariableAccess {
40+
AtomicVariableAccess() { getTarget().getType().hasSpecifier("atomic") }
41+
42+
/* Get the `atomic_<read|write>()` call this VarAccess occurs in. */
43+
FunctionCall getAtomicFunctionCall() {
44+
exists(AddressOfExpr addrParent, FunctionCall fc |
45+
fc.getTarget().getName().matches("__c11_atomic%") and
46+
addrParent = fc.getArgument(0) and
47+
addrParent.getAnOperand() = this and
48+
result = fc
49+
)
50+
}
51+
52+
/**
53+
* Gets an assigned expr, either in the form `x = <result>` or `atomic_store(&x, <result>)`.
54+
*/
55+
Expr getAnAssignedExpr() {
56+
result = getAtomicFunctionCall().getArgument(1)
57+
or
58+
exists(AssignExpr assign |
59+
assign.getLValue() = this and
60+
result = assign.getRValue()
61+
)
62+
}
63+
64+
/**
65+
* Gets the expression holding this variable access, either in the form `x` or `atomic_read(&x)`.
66+
*/
67+
Expr getARead() {
68+
result = getAtomicFunctionCall()
69+
or
70+
result = this
71+
}
72+
}
73+
74+
from
75+
AtomicAccessInFullExpressionOrdering config, FullExpr e, Variable v, AtomicVariableAccess va1,
76+
AtomicVariableAccess va2
77+
where
78+
not isExcluded(e, SideEffects3Package::unsequencedAtomicReadsQuery()) and
79+
e = va1.(ConstituentExpr).getFullExpr() and
80+
config.isUnsequenced(va1, va2) and
81+
v = va1.getTarget() and
82+
v = va2.getTarget() and
83+
// Exclude cases where the variable is assigned a value tainted by the other variable access.
84+
not exists(Expr write |
85+
write = va1.getAnAssignedExpr() and
86+
TaintTracking::localTaint(DataFlow::exprNode(va2.getARead()), DataFlow::exprNode(write))
87+
) and
88+
// Impose an ordering, show the first access.
89+
va1.getLocation().isBefore(va2.getLocation(), _)
90+
select e, "Atomic variable $@ has a $@ that is unsequenced with $@.", v, v.getName(), va1,
91+
"previous read", va2, "another read"

0 commit comments

Comments
 (0)