Skip to content

Commit 767d314

Browse files
authored
Merge pull request github#5336 from MathiasVP/fix-join-order-in-memset-may-be-deleted
C++: Fix performance in cpp/memset-may-be-deleted.
2 parents 220383b + 2d7f15c commit 767d314

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

cpp/ql/src/Security/CWE/CWE-014/MemsetMayBeDeleted.ql

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,39 @@ predicate isNonEscapingArgument(Expr escaped) {
3838
)
3939
}
4040

41+
pragma[noinline]
42+
predicate callToMemsetWithRelevantVariable(
43+
LocalVariable v, VariableAccess acc, FunctionCall call, MemsetFunction memset
44+
) {
45+
not v.isStatic() and
46+
// Reference-typed variables get special treatment in `variableAddressEscapesTree` so we leave them
47+
// out of this query.
48+
not v.getUnspecifiedType() instanceof ReferenceType and
49+
call.getTarget() = memset and
50+
acc = v.getAnAccess() and
51+
// `v` escapes as the argument to `memset`
52+
variableAddressEscapesTree(acc, call.getArgument(0).getFullyConverted())
53+
}
54+
55+
pragma[noinline]
56+
predicate relevantVariable(LocalVariable v, FunctionCall call, MemsetFunction memset) {
57+
exists(VariableAccess acc, VariableAccess anotherAcc |
58+
callToMemsetWithRelevantVariable(v, acc, call, memset) and
59+
// `v` is not only just used in the call to `memset`.
60+
anotherAcc = v.getAnAccess() and
61+
acc != anotherAcc and
62+
not anotherAcc.isUnevaluated()
63+
)
64+
}
65+
4166
from FunctionCall call, LocalVariable v, MemsetFunction memset
4267
where
43-
call.getTarget() = memset and
68+
relevantVariable(v, call, memset) and
4469
not isFromMacroDefinition(call) and
45-
// `v` escapes as the argument to `memset`
46-
variableAddressEscapesTree(v.getAnAccess(), call.getArgument(0).getFullyConverted()) and
47-
// ... and `v` doesn't escape anywhere else.
70+
// `v` doesn't escape anywhere else.
4871
forall(Expr escape | variableAddressEscapesTree(v.getAnAccess(), escape) |
4972
isNonEscapingArgument(escape)
5073
) and
51-
not v.isStatic() and
52-
// Reference-typed variables get special treatment in `variableAddressEscapesTree` so we leave them
53-
// out of this query.
54-
not v.getUnspecifiedType() instanceof ReferenceType and
55-
// `v` is not only just used in the call to `memset`.
56-
exists(Access acc |
57-
acc = v.getAnAccess() and not call.getArgument(0).getAChild*() = acc and not acc.isUnevaluated()
58-
) and
5974
// There is no later use of `v`.
6075
not v.getAnAccess() = call.getASuccessor*() and
6176
// Not using the `-fno-builtin-memset` flag

0 commit comments

Comments
 (0)