Skip to content

Commit 630982c

Browse files
authored
C++: auto format Printf.qll
1 parent 9f811b2 commit 630982c

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

cpp/ql/lib/semmle/code/cpp/commons/Printf.qll

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ private newtype TBufferWriteEstimationReason =
1717

1818
private predicate gradeToReason(int grade, TBufferWriteEstimationReason reason) {
1919
// when combining reasons, lower grade takes precedence
20-
grade = 0 and reason = TUnspecifiedEstimateReason() or
21-
grade = 1 and reason = TTypeBoundsAnalysis() or
22-
grade = 2 and reason = TWidenedValueFlowAnalysis() or
20+
grade = 0 and reason = TUnspecifiedEstimateReason()
21+
or
22+
grade = 1 and reason = TTypeBoundsAnalysis()
23+
or
24+
grade = 2 and reason = TWidenedValueFlowAnalysis()
25+
or
2326
grade = 3 and reason = TValueFlowAnalysis()
2427
}
2528

@@ -42,8 +45,10 @@ abstract class BufferWriteEstimationReason extends TBufferWriteEstimationReason
4245
* conversion given reasons coming from its individual specifiers.
4346
*/
4447
BufferWriteEstimationReason combineWith(BufferWriteEstimationReason other) {
45-
exists(int grade, int otherGrade | gradeToReason(grade, this) and gradeToReason(otherGrade, other) |
46-
if otherGrade < grade then result = other else result = this
48+
exists(int grade, int otherGrade |
49+
gradeToReason(grade, this) and gradeToReason(otherGrade, other)
50+
|
51+
if otherGrade < grade then result = other else result = this
4752
)
4853
}
4954
}
@@ -83,10 +88,11 @@ class TypeBoundsAnalysis extends BufferWriteEstimationReason, TTypeBoundsAnalysi
8388
class WidenedValueFlowAnalysis extends BufferWriteEstimationReason, TWidenedValueFlowAnalysis {
8489
override string toString() { result = "WidenedValueFlowAnalysis" }
8590

86-
override string getDescription() { result = "based on flow analysis of value bounds with a widening approximation" }
91+
override string getDescription() {
92+
result = "based on flow analysis of value bounds with a widening approximation"
93+
}
8794
}
8895

89-
9096
/**
9197
* The estimation comes from non trivial bounds found via actual flow analysis.
9298
* For example
@@ -381,12 +387,17 @@ private BufferWriteEstimationReason getEstimationReasonForIntegralExpression(Exp
381387
// * constrained non-trivially both sides of a signed value, or
382388
// * constrained non-trivially the positive side of an unsigned value
383389
// expr should already be given as getFullyConverted
384-
if upperBound(expr) < exprMaxVal(expr) and (exprMinVal(expr) >= 0 or lowerBound(expr) > exprMinVal(expr))
385-
// next we check whether the estimate may have been widened
386-
then if upperBoundMayBeWidened(expr) then result = TWidenedValueFlowAnalysis()
387-
else result = TValueFlowAnalysis()
390+
if
391+
upperBound(expr) < exprMaxVal(expr) and
392+
(exprMinVal(expr) >= 0 or lowerBound(expr) > exprMinVal(expr))
393+
then
394+
// next we check whether the estimate may have been widened
395+
if upperBoundMayBeWidened(expr)
396+
then result = TWidenedValueFlowAnalysis()
397+
else result = TValueFlowAnalysis()
388398
else result = TTypeBoundsAnalysis()
389399
}
400+
390401
/**
391402
* A class to represent format strings that occur as arguments to invocations of formatting functions.
392403
*/

0 commit comments

Comments
 (0)