Skip to content

Commit f1ee7e9

Browse files
committed
Merge branch 'main' into next
2 parents 4efa59d + 3734236 commit f1ee7e9

File tree

72 files changed

+853
-431
lines changed

Some content is hidden

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

72 files changed

+853
-431
lines changed

.vscode/tasks.json

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
"Null",
272272
"OperatorInvariants",
273273
"Operators",
274+
"OrderOfEvaluation",
274275
"OutOfBounds",
275276
"Pointers",
276277
"Pointers1",

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ This repository contains CodeQL queries and libraries which support various Codi
66

77
_Carnegie Mellon and CERT are registered trademarks of Carnegie Mellon University._
88

9-
This repository contains CodeQL queries and libraries which support various Coding Standards for the [C++14](https://www.iso.org/standard/64029.html) programming language.
9+
This repository contains CodeQL queries and libraries which support various Coding Standards for the [C++14](https://www.iso.org/standard/64029.html), [C99](https://www.iso.org/standard/29237.html) and [C11](https://www.iso.org/standard/57853.html) programming languages.
1010

1111
The following coding standards are supported:
1212
- [AUTOSAR - Guidelines for the use of C++14 language in critical and safety-related systems (Releases R22-11, R20-11, R19-11 and R19-03)](https://www.autosar.org/fileadmin/standards/R22-11/AP/AUTOSAR_RS_CPP14Guidelines.pdf).
1313
- [MISRA C++:2008](https://www.misra.org.uk) (support limited to the rules specified in AUTOSAR).
1414
- [SEI CERT C++ Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition)](https://resources.sei.cmu.edu/library/asset-view.cfm?assetID=494932)
15-
16-
In addition, the following Coding Standards for the C programming language are under development:
17-
1815
- [SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition)](https://resources.sei.cmu.edu/downloads/secure-coding/assets/sei-cert-c-coding-standard-2016-v01.pdf)
1916
- [MISRA C 2012](https://www.misra.org.uk/product/misra-c2012-third-edition-first-revision/).
2017

c/cert/src/rules/INT34-C/ExprShiftedbyNegativeOrGreaterPrecisionOperand.ql

+4-87
Original file line numberDiff line numberDiff line change
@@ -15,91 +15,8 @@ import codingstandards.c.cert
1515
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1616
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1717
import semmle.code.cpp.controlflow.Guards
18+
import codingstandards.cpp.UndefinedBehavior
1819

19-
/*
20-
* Precision predicate based on a sample implementation from
21-
* https://wiki.sei.cmu.edu/confluence/display/c/INT35-C.+Use+correct+integer+precisions
22-
*/
23-
24-
/**
25-
* A function whose name is suggestive that it counts the number of bits set.
26-
*/
27-
class PopCount extends Function {
28-
PopCount() { this.getName().toLowerCase().matches("%popc%nt%") }
29-
}
30-
31-
/**
32-
* A macro which is suggestive that it is used to determine the precision of an integer.
33-
*/
34-
class PrecisionMacro extends Macro {
35-
PrecisionMacro() { this.getName().toLowerCase().matches("precision") }
36-
}
37-
38-
class LiteralZero extends Literal {
39-
LiteralZero() { this.getValue() = "0" }
40-
}
41-
42-
class BitShiftExpr extends BinaryBitwiseOperation {
43-
BitShiftExpr() {
44-
this instanceof LShiftExpr or
45-
this instanceof RShiftExpr
46-
}
47-
}
48-
49-
int getPrecision(IntegralType type) {
50-
type.isExplicitlyUnsigned() and result = type.getSize() * 8
51-
or
52-
type.isExplicitlySigned() and result = type.getSize() * 8 - 1
53-
}
54-
55-
predicate isForbiddenShiftExpr(BitShiftExpr shift, string message) {
56-
(
57-
(
58-
getPrecision(shift.getLeftOperand().getExplicitlyConverted().getUnderlyingType()) <=
59-
upperBound(shift.getRightOperand()) and
60-
message =
61-
"The operand " + shift.getLeftOperand() + " is shifted by an expression " +
62-
shift.getRightOperand() + " whose upper bound (" + upperBound(shift.getRightOperand()) +
63-
") is greater than or equal to the precision."
64-
or
65-
lowerBound(shift.getRightOperand()) < 0 and
66-
message =
67-
"The operand " + shift.getLeftOperand() + " is shifted by an expression " +
68-
shift.getRightOperand() + " which may be negative."
69-
) and
70-
/*
71-
* Shift statement is not at a basic block where
72-
* `shift_rhs < PRECISION(...)` is ensured
73-
*/
74-
75-
not exists(GuardCondition gc, BasicBlock block, Expr precisionCall, Expr lTLhs |
76-
block = shift.getBasicBlock() and
77-
(
78-
precisionCall.(FunctionCall).getTarget() instanceof PopCount
79-
or
80-
precisionCall = any(PrecisionMacro pm).getAnInvocation().getExpr()
81-
)
82-
|
83-
globalValueNumber(lTLhs) = globalValueNumber(shift.getRightOperand()) and
84-
gc.ensuresLt(lTLhs, precisionCall, 0, block, true)
85-
) and
86-
/*
87-
* Shift statement is not at a basic block where
88-
* `shift_rhs < 0` is ensured
89-
*/
90-
91-
not exists(GuardCondition gc, BasicBlock block, Expr literalZero, Expr lTLhs |
92-
block = shift.getBasicBlock() and
93-
literalZero instanceof LiteralZero
94-
|
95-
globalValueNumber(lTLhs) = globalValueNumber(shift.getRightOperand()) and
96-
gc.ensuresLt(lTLhs, literalZero, 0, block, true)
97-
)
98-
)
99-
}
100-
101-
from BinaryBitwiseOperation badShift, string message
102-
where
103-
not isExcluded(badShift, Types1Package::exprShiftedbyNegativeOrGreaterPrecisionOperandQuery()) and
104-
isForbiddenShiftExpr(badShift, message)
105-
select badShift, message
20+
from ShiftByNegativeOrGreaterPrecisionOperand badShift
21+
where not isExcluded(badShift, Types1Package::exprShiftedbyNegativeOrGreaterPrecisionOperandQuery())
22+
select badShift, badShift.getReason()

0 commit comments

Comments
 (0)