Skip to content

Commit 20bc3ab

Browse files
committed
Some minor comment adjustments
1 parent 2c36364 commit 20bc3ab

File tree

2 files changed

+27
-46
lines changed

2 files changed

+27
-46
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
import cpp
1414
import codingstandards.c.cert
1515

16-
/* Precision predicate based on a sample implementaion from https://wiki.sei.cmu.edu/confluence/display/c/INT35-C.+Use+correct+integer+precisions */
16+
/*
17+
* Precision predicate based on a sample implementaion from
18+
* https://wiki.sei.cmu.edu/confluence/display/c/INT35-C.+Use+correct+integer+precisions
19+
*/
20+
1721
int getPrecision(BuiltInType type) {
1822
type.(CharType).isExplicitlyUnsigned() and result = 8
1923
or
@@ -36,6 +40,7 @@ int getPrecision(BuiltInType type) {
3640
type instanceof LongLongType and not type.(LongLongType).isExplicitlyUnsigned() and result = 63
3741
}
3842

43+
/* The -1 number literal. */
3944
class MinusNumberLiteral extends UnaryMinusExpr {
4045
MinusNumberLiteral() { this.getOperand() instanceof Literal }
4146

@@ -45,17 +50,18 @@ class MinusNumberLiteral extends UnaryMinusExpr {
4550
class ForbiddenShiftExpr extends BinaryBitwiseOperation {
4651
ForbiddenShiftExpr() {
4752
(
48-
/* Precision mismatch between operands */
53+
/* First Case: Precision mismatch between operands */
4954
getPrecision(this.(LShiftExpr).getLeftOperand().getUnderlyingType()) <=
5055
getPrecision(this.(LShiftExpr).getRightOperand().getUnderlyingType()) or
5156
getPrecision(this.(RShiftExpr).getLeftOperand().getUnderlyingType()) <=
5257
getPrecision(this.(RShiftExpr).getRightOperand().getUnderlyingType()) or
53-
/* Shifting by a negative number literal */
58+
/* Second Case: Shifting by a negative number literal */
5459
this.(LShiftExpr).getRightOperand() instanceof MinusNumberLiteral or
5560
this.(RShiftExpr).getRightOperand() instanceof MinusNumberLiteral
5661
)
5762
}
5863

64+
/* Second Case: Shifting by a negative number literal */
5965
predicate hasNegativeOperand() {
6066
this.(LShiftExpr).getRightOperand() instanceof MinusNumberLiteral or
6167
this.(RShiftExpr).getRightOperand() instanceof MinusNumberLiteral

c/misra/src/rules/RULE-7-4/StringLiteralAssignedToNonConstChar.ql

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,80 +21,55 @@ class NonConstCharStarType extends Type {
2121
}
2222

2323
/* A non-const-char* variable declared with a string literal */
24-
predicate declaringNonConstCharVar(Variable decl) {
24+
predicate declaringNonConstCharVar(Variable decl, string message) {
2525
not decl instanceof Parameter and // exclude parameters
2626
/* It should be declaring a char* type variable */
2727
decl.getUnspecifiedType() instanceof CharPointerType and
2828
not decl.getUnderlyingType().isDeeplyConstBelow() and
2929
/* But it's declared to hold a string literal. */
30-
decl.getInitializer().getExpr() instanceof StringLiteral
30+
decl.getInitializer().getExpr() instanceof StringLiteral and
31+
message = "char* variable " + decl + " is declared with a string literal."
3132
}
3233

3334
/* String literal being assigned to a non-const-char* variable */
34-
predicate assignmentToNonConstCharVar(Assignment assign) {
35+
predicate assignmentToNonConstCharVar(Assignment assign, string message) {
3536
/* The variable being assigned is char* */
3637
assign.getLValue().getUnderlyingType() instanceof NonConstCharStarType and
3738
/* But the rvalue is a string literal */
38-
exists(Expr rvalue | rvalue = assign.getRValue() | rvalue instanceof StringLiteral)
39+
exists(Expr rvalue | rvalue = assign.getRValue() | rvalue instanceof StringLiteral) and
40+
message = "char* variable " + assign.getLValue() + " is assigned a string literal. "
3941
}
4042

4143
/* String literal being passed to a non-const-char* parameter */
42-
predicate assignmentToNonConstCharParam(FunctionCall call) {
44+
predicate assignmentToNonConstCharParam(FunctionCall call, string message) {
4345
exists(int index |
4446
/* Param at index is a char* */
4547
call.getTarget().getParameter(index).getUnderlyingType() instanceof NonConstCharStarType and
4648
/* But a string literal is passed */
4749
call.getArgument(index) instanceof StringLiteral
48-
)
50+
) and
51+
message = "char* parameter of " + call.getTarget() + " is passed a string literal."
4952
}
5053

5154
/* String literal being returned by a non-const-char* function */
52-
predicate returningNonConstCharVar(ReturnStmt return) {
55+
predicate returningNonConstCharVar(ReturnStmt return, string message) {
5356
/* The function is declared to return a char* */
5457
return.getEnclosingFunction().getType().resolveTypedefs() instanceof NonConstCharStarType and
5558
/* But in reality it returns a string literal */
56-
return.getExpr() instanceof StringLiteral
59+
return.getExpr() instanceof StringLiteral and
60+
message = "char* function " + return.getEnclosingFunction() + " is returning a string literal."
5761
}
5862

59-
// newtype TProblematicElem =
60-
// TVar(Variable decl) or
61-
// TAssign(Assignment assign) or
62-
// TFunCall(FunctionCall call) or
63-
// TReturnStmt(ReturnStmt return)
64-
// class ProblematicElem extends TProblematicElem {
65-
// Variable getVariable() { this = TVar(result) }
66-
// Assignment getAssign() { this = TAssign(result) }
67-
// FunctionCall getFunCall() { this = TFunCall(result) }
68-
// ReturnStmt getReturnStmt() { this = TReturnStmt(result) }
69-
// override string toString() {
70-
// this instanceof TVar and result = this.getVariable().toString()
71-
// or
72-
// this instanceof TAssign and result = this.getAssign().toString()
73-
// or
74-
// this instanceof TFunCall and result = this.getFunCall().toString()
75-
// or
76-
// this instanceof TReturnStmt and result = this.getReturnStmt().toString()
77-
// }
78-
// }
79-
// class ProblematicElem = Variable or Assignment or FunctionCall or ReturnStmt;
80-
// ^ Nope!
81-
from Variable decl, Assignment assign, FunctionCall call, ReturnStmt return, string message
63+
from Element elem, string message
8264
where
83-
not isExcluded(decl, TypesPackage::stringLiteralAssignedToNonConstCharQuery()) and
84-
not isExcluded(assign, TypesPackage::stringLiteralAssignedToNonConstCharQuery()) and
85-
not isExcluded(call, TypesPackage::stringLiteralAssignedToNonConstCharQuery()) and
86-
not isExcluded(return, TypesPackage::stringLiteralAssignedToNonConstCharQuery()) and
65+
not isExcluded(elem, TypesPackage::stringLiteralAssignedToNonConstCharQuery()) and
8766
(
88-
declaringNonConstCharVar(decl) and
89-
message = "char* variable " + decl + " is declared with a string literal."
67+
declaringNonConstCharVar(elem, message)
9068
or
91-
assignmentToNonConstCharVar(assign) and
92-
message = "char* variable " + assign.getLValue() + " is assigned a string literal. "
69+
assignmentToNonConstCharVar(elem, message)
9370
or
94-
assignmentToNonConstCharParam(call) and
95-
message = "char* parameter of " + call.getTarget() + " is passed a string literal."
71+
assignmentToNonConstCharParam(elem, message)
9672
or
97-
returningNonConstCharVar(return) and
98-
message = "char* function " + return.getEnclosingFunction() + " is returning a string literal."
73+
returningNonConstCharVar(elem, message)
9974
)
10075
select message

0 commit comments

Comments
 (0)