Skip to content

Commit c4b01d0

Browse files
committed
CPP: Autoformat some other untidy source files.
1 parent 346bc1a commit c4b01d0

File tree

3 files changed

+80
-65
lines changed

3 files changed

+80
-65
lines changed

cpp/ql/src/Header Cleanup/Cleanup-DuplicateIncludeGuard.ql

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* maintainability
1010
* modularity
1111
*/
12+
1213
import cpp
1314
import semmle.code.cpp.headers.MultipleInclusion
1415

@@ -20,9 +21,15 @@ import semmle.code.cpp.headers.MultipleInclusion
2021
* However one case must be a correctIncludeGuard to prove that this macro really is intended
2122
* to be an include guard.
2223
*/
24+
2325
from HeaderFile hf, PreprocessorDirective ifndef, string macroName, int num
24-
where hasIncludeGuard(hf, ifndef, _, macroName)
25-
and exists(HeaderFile other | hasIncludeGuard(other, _, _, macroName) and hf.getShortName() != other.getShortName())
26-
and num = strictcount(HeaderFile other | hasIncludeGuard(other, _, _, macroName))
27-
and correctIncludeGuard(_, _, _, _, macroName)
28-
select ifndef, "The macro name '" + macroName + "' of this include guard is used in " + num + " different header files."
26+
where
27+
hasIncludeGuard(hf, ifndef, _, macroName) and
28+
exists(HeaderFile other |
29+
hasIncludeGuard(other, _, _, macroName) and hf.getShortName() != other.getShortName()
30+
) and
31+
num = strictcount(HeaderFile other | hasIncludeGuard(other, _, _, macroName)) and
32+
correctIncludeGuard(_, _, _, _, macroName)
33+
select ifndef,
34+
"The macro name '" + macroName + "' of this include guard is used in " + num +
35+
" different header files."
Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @name Potentially overflowing call to snprintf
33
* @description Using the return value from snprintf without proper checks can cause overflow.
4-
*
54
* @kind problem
65
* @problem.severity warning
76
* @precision high
@@ -20,44 +19,44 @@ import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
2019
* true if there is an arithmetic operation on the path that
2120
* might overflow.
2221
*/
23-
predicate flowsToExpr(
24-
Expr source, Expr sink, boolean pathMightOverflow) {
22+
predicate flowsToExpr(Expr source, Expr sink, boolean pathMightOverflow) {
2523
// Might the current expression overflow?
26-
exists (boolean otherMightOverflow
27-
| flowsToExprImpl(source, sink, otherMightOverflow)
28-
| if convertedExprMightOverflow(sink)
29-
then pathMightOverflow = true
30-
else pathMightOverflow = otherMightOverflow)
24+
exists(boolean otherMightOverflow | flowsToExprImpl(source, sink, otherMightOverflow) |
25+
if convertedExprMightOverflow(sink)
26+
then pathMightOverflow = true
27+
else pathMightOverflow = otherMightOverflow
28+
)
3129
}
3230

3331
/**
3432
* Implementation of `flowsToExpr`. Does everything except
3533
* checking whether the current expression might overflow.
3634
*/
37-
predicate flowsToExprImpl(
38-
Expr source, Expr sink, boolean pathMightOverflow) {
39-
(source = sink and pathMightOverflow = false and
40-
source.(FunctionCall).getTarget().(Snprintf).returnsFullFormatLength())
35+
predicate flowsToExprImpl(Expr source, Expr sink, boolean pathMightOverflow) {
36+
(
37+
source = sink and
38+
pathMightOverflow = false and
39+
source.(FunctionCall).getTarget().(Snprintf).returnsFullFormatLength()
40+
)
4141
or
42-
exists (RangeSsaDefinition def, LocalScopeVariable v
43-
| flowsToDef(source, def, v, pathMightOverflow) and
44-
sink = def.getAUse(v))
42+
exists(RangeSsaDefinition def, LocalScopeVariable v |
43+
flowsToDef(source, def, v, pathMightOverflow) and
44+
sink = def.getAUse(v)
45+
)
4546
or
46-
flowsToExpr(
47-
source, sink.(UnaryArithmeticOperation).getOperand(), pathMightOverflow)
47+
flowsToExpr(source, sink.(UnaryArithmeticOperation).getOperand(), pathMightOverflow)
4848
or
49-
flowsToExpr(
50-
source, sink.(BinaryArithmeticOperation).getAnOperand(), pathMightOverflow)
49+
flowsToExpr(source, sink.(BinaryArithmeticOperation).getAnOperand(), pathMightOverflow)
5150
or
52-
flowsToExpr(
53-
source, sink.(Assignment).getRValue(), pathMightOverflow)
51+
flowsToExpr(source, sink.(Assignment).getRValue(), pathMightOverflow)
5452
or
55-
flowsToExpr(
56-
source, sink.(AssignOperation).getLValue(), pathMightOverflow)
53+
flowsToExpr(source, sink.(AssignOperation).getLValue(), pathMightOverflow)
5754
or
58-
exists (FormattingFunctionCall call
59-
| sink = call and
60-
flowsToExpr(source, call.getArgument(call.getTarget().getSizeParameterIndex()), pathMightOverflow))
55+
exists(FormattingFunctionCall call |
56+
sink = call and
57+
flowsToExpr(source, call.getArgument(call.getTarget().getSizeParameterIndex()),
58+
pathMightOverflow)
59+
)
6160
}
6261

6362
/**
@@ -67,14 +66,14 @@ predicate flowsToExprImpl(
6766
* on the path that might overflow.
6867
*/
6968
predicate flowsToDef(
70-
Expr source, RangeSsaDefinition def, LocalScopeVariable v,
71-
boolean pathMightOverflow) {
69+
Expr source, RangeSsaDefinition def, LocalScopeVariable v, boolean pathMightOverflow
70+
) {
7271
// Might the current definition overflow?
73-
exists (boolean otherMightOverflow
74-
| flowsToDefImpl(source, def, v, otherMightOverflow)
75-
| if defMightOverflow(def, v)
76-
then pathMightOverflow = true
77-
else pathMightOverflow = otherMightOverflow)
72+
exists(boolean otherMightOverflow | flowsToDefImpl(source, def, v, otherMightOverflow) |
73+
if defMightOverflow(def, v)
74+
then pathMightOverflow = true
75+
else pathMightOverflow = otherMightOverflow
76+
)
7877
}
7978

8079
/**
@@ -89,26 +88,29 @@ predicate flowsToDef(
8988
* the path. But it is a good way to reduce the number of false positives.
9089
*/
9190
predicate flowsToDefImpl(
92-
Expr source, RangeSsaDefinition def, LocalScopeVariable v,
93-
boolean pathMightOverflow) {
91+
Expr source, RangeSsaDefinition def, LocalScopeVariable v, boolean pathMightOverflow
92+
) {
9493
// Assignment or initialization: `e = v;`
95-
exists (Expr e
96-
| e = def.getDefiningValue(v) and
97-
flowsToExpr(source, e, pathMightOverflow))
94+
exists(Expr e |
95+
e = def.getDefiningValue(v) and
96+
flowsToExpr(source, e, pathMightOverflow)
97+
)
9898
or
9999
// `x++`
100-
exists (CrementOperation crem
101-
| def = crem and
100+
exists(CrementOperation crem |
101+
def = crem and
102102
crem.getOperand() = v.getAnAccess() and
103-
flowsToExpr(source, crem.getOperand(), pathMightOverflow))
103+
flowsToExpr(source, crem.getOperand(), pathMightOverflow)
104+
)
104105
or
105106
// Phi definition.
106107
flowsToDef(source, def.getAPhiInput(v), v, pathMightOverflow)
107108
}
108109

109110
from FormattingFunctionCall call, Expr sink
110-
where flowsToExpr(call, sink, true)
111-
and sink = call.getArgument(call.getTarget().getSizeParameterIndex())
112-
select
113-
call, "The $@ of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow.",
111+
where
112+
flowsToExpr(call, sink, true) and
113+
sink = call.getArgument(call.getTarget().getSizeParameterIndex())
114+
select call,
115+
"The $@ of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow.",
114116
sink, "size argument"

cpp/ql/src/Likely Bugs/Likely Typos/LogicalExprCouldBeSimplified.ql

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@
88
* @problem.severity warning
99
* @tags maintainability
1010
*/
11+
1112
import cpp
1213

1314
/**
1415
* A simple literal (i.e. not a macro expansion, enum constant
1516
* or template argument).
1617
*/
1718
predicate simple(Literal l) {
18-
l instanceof OctalLiteral or
19-
l instanceof HexLiteral or
20-
l instanceof CharLiteral or
21-
l.getValueText() = "true" or
22-
l.getValueText() = "false" or
23-
// Parsing doubles is too slow...
24-
//exists(l.getValueText().toFloat())
25-
// Instead, check whether the literal starts with a letter.
26-
not l.getValueText().regexpMatch("[a-zA-Z_].*")
19+
l instanceof OctalLiteral or
20+
l instanceof HexLiteral or
21+
l instanceof CharLiteral or
22+
l.getValueText() = "true" or
23+
l.getValueText() = "false" or
24+
// Parsing doubles is too slow...
25+
//exists(l.getValueText().toFloat())
26+
// Instead, check whether the literal starts with a letter.
27+
not l.getValueText().regexpMatch("[a-zA-Z_].*")
2728
}
2829

2930
predicate booleanLiteral(Literal l) {
@@ -32,18 +33,23 @@ predicate booleanLiteral(Literal l) {
3233
}
3334

3435
string boolLiteralInLogicalOp(Literal literal) {
35-
booleanLiteral(literal) and literal.getParent() instanceof BinaryLogicalOperation and
36-
result = "Literal value " + literal.getValueText() + " is used in a logical expression; simplify or use a constant."
36+
booleanLiteral(literal) and
37+
literal.getParent() instanceof BinaryLogicalOperation and
38+
result = "Literal value " + literal.getValueText() +
39+
" is used in a logical expression; simplify or use a constant."
3740
}
3841

3942
string comparisonOnLiterals(ComparisonOperation op) {
40-
simple(op.getLeftOperand()) and simple(op.getRightOperand()) and
43+
simple(op.getLeftOperand()) and
44+
simple(op.getRightOperand()) and
4145
not op.getAnOperand().isInMacroExpansion() and
42-
if op.isConstant() then result = "This comparison involves two literals and is always " + op.getValue() + "."
46+
if op.isConstant()
47+
then result = "This comparison involves two literals and is always " + op.getValue() + "."
4348
else result = "This comparison involves two literals and should be simplified."
4449
}
4550

4651
from Expr e, string msg
47-
where (msg = boolLiteralInLogicalOp(e) or msg = comparisonOnLiterals(e)) and
48-
not e.isInMacroExpansion()
52+
where
53+
(msg = boolLiteralInLogicalOp(e) or msg = comparisonOnLiterals(e)) and
54+
not e.isInMacroExpansion()
4955
select e, msg

0 commit comments

Comments
 (0)