Skip to content

Commit 202fb66

Browse files
Address feedback.
1 parent 07d28ef commit 202fb66

File tree

5 files changed

+15
-22
lines changed

5 files changed

+15
-22
lines changed

c/misra/src/rules/RULE-7-5/IncorrectlySizedIntegerConstantMacroArgument.ql

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ predicate matchesSign(IntegerConstantMacro macro, PossiblyNegativeLiteral litera
2121
}
2222

2323
predicate matchesSize(IntegerConstantMacro macro, PossiblyNegativeLiteral literal) {
24-
// Wait for BigInt support to check 64 bit macro types.
25-
(macro.getSize() < 64 and matchesSign(macro, literal))
26-
implies
27-
(
28-
literal.getRawValue() <= macro.maxValue() and
29-
literal.getRawValue() >= macro.minValue()
30-
)
24+
literal.getRawValue() <= macro.maxValue() and
25+
literal.getRawValue() >= macro.minValue()
3126
}
3227

3328
from
@@ -38,9 +33,13 @@ where
3833
invoke.getMacro() = macro and
3934
literal = invoke.getExpr() and
4035
(
41-
not matchesSign(macro, invoke.getExpr()) and explanation = " cannot be negative"
36+
not matchesSign(macro, literal) and
37+
explanation = " cannot be negative"
4238
or
43-
not matchesSize(macro, invoke.getExpr()) and
39+
matchesSign(macro, literal) and
40+
// Wait for BigInt support to check 64 bit macro types.
41+
macro.getSize() < 64 and
42+
not matchesSize(macro, literal) and
4443
explanation = " is outside of the allowed range " + macro.getRangeString()
4544
)
46-
select invoke.getExpr(), "Value provided to integer constant macro " + macro.getName() + explanation
45+
select literal, "Value provided to integer constant macro " + macro.getName() + explanation

c/misra/src/rules/RULE-7-5/IntegerConstantMacroArgumentUsesSuffix.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import codingstandards.cpp.IntegerConstantMacro
1818
import codingstandards.cpp.Literals
1919

2020
string argumentSuffix(MacroInvocation invoke) {
21-
// Compiler strips the suffix unless we look at the unexpanded argument text.
21+
// Extractor strips the suffix unless we look at the unexpanded argument text.
2222
// Unexpanded argument text can be malformed in all sorts of ways, so make
2323
// this match relatively strict, to be safe.
2424
result = invoke.getUnexpandedArgument(0).regexpCapture("([0-9]+|0[xX][0-9A-F]+)([uUlL]+)$", 2)
@@ -30,6 +30,6 @@ where
3030
invoke.getMacro() instanceof IntegerConstantMacro and
3131
invoke.getExpr() = argument and
3232
suffix = argumentSuffix(invoke)
33-
select invoke.getExpr(),
33+
select argument,
3434
"Value suffix '" + suffix + "' is not allowed on provided argument to integer constant macro " +
3535
invoke.getMacroName() + "."

c/misra/src/rules/RULE-7-5/InvalidIntegerConstantMacroArgument.ql

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ import codingstandards.cpp.IntegerConstantMacro
1717
import codingstandards.cpp.Literals
1818
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1919

20-
predicate containsMacroInvocation(MacroInvocation outer, MacroInvocation inner) {
21-
outer.getExpr() = inner.getExpr() and
22-
exists(outer.getUnexpandedArgument(0).indexOf(inner.getMacroName()))
23-
}
24-
2520
from MacroInvocation invoke, IntegerConstantMacro macro
2621
where
2722
not isExcluded(invoke, Types2Package::invalidIntegerConstantMacroArgumentQuery()) and
2823
invoke.getMacro() = macro and
2924
(
3025
not invoke.getExpr() instanceof PossiblyNegativeLiteral
3126
or
32-
containsMacroInvocation(invoke, _)
27+
any(MacroInvocation inner).getParentInvocation() = invoke
3328
)
3429
select invoke.getExpr(),
3530
"Argument to integer constant macro " + macro.getName() + " must be an integer literal."

cpp/common/src/codingstandards/cpp/IntegerConstantMacro.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class IntegerConstantMacro extends Macro {
1515
signed = false and size = getName().regexpCapture("UINT(8|16|32|64)_C", 1).toInt()
1616
}
1717

18-
predicate isSmall() { size < 32 }
18+
predicate isSmall() { size < any(IntType it | it.isSigned()).getSize() }
1919

2020
int getSize() { result = size }
2121

cpp/common/src/codingstandards/cpp/Literals.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import cpp
66
import codingstandards.cpp.Cpp14Literal
7-
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
87

98
class IntegerLiteral = Cpp14Literal::IntegerLiteral;
109

@@ -100,7 +99,7 @@ class NegativeLiteral extends PossiblyNegativeLiteral, UnaryMinusExpr {
10099

101100
override Cpp14Literal::NumericLiteral getBaseLiteral() { result = literal }
102101

103-
override float getRawValue() { result = -lowerBound(literal) }
102+
override float getRawValue() { result = -literal.getValue().toFloat() }
104103
}
105104

106105
/**
@@ -112,5 +111,5 @@ class PositiveLiteral extends PossiblyNegativeLiteral, Cpp14Literal::NumericLite
112111

113112
override Cpp14Literal::NumericLiteral getBaseLiteral() { result = this }
114113

115-
override float getRawValue() { result = lowerBound(this) }
114+
override float getRawValue() { result = getValue().toFloat() }
116115
}

0 commit comments

Comments
 (0)