Skip to content

Commit 2345907

Browse files
committed
C++: Reintroduce the 'cannotContainString' optimization that was removed in #15516.
1 parent f97b6e2 commit 2345907

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

cpp/ql/src/Likely Bugs/Format/NonConstantFormat.ql

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ class UncalledFunction extends Function {
3737
}
3838
}
3939

40+
/**
41+
* Holds if `t` cannot refer to a string. That is, it's a built-in
42+
* or arithmetic type that is not a "`char` like" type.
43+
*/
44+
predicate cannotContainString(Type t) {
45+
exists(Type unspecified |
46+
unspecified = t.getUnspecifiedType() and
47+
not unspecified instanceof UnknownType and
48+
not unspecified instanceof CharType and
49+
not unspecified instanceof WideCharType and
50+
not unspecified instanceof Char8Type and
51+
not unspecified instanceof Char16Type and
52+
not unspecified instanceof Char32Type
53+
|
54+
unspecified instanceof ArithmeticType or
55+
unspecified instanceof BuiltInType
56+
)
57+
}
58+
4059
predicate dataFlowOrTaintFlowFunction(Function func, FunctionOutput output) {
4160
func.(DataFlowFunction).hasDataFlow(_, output) or
4261
func.(TaintFunction).hasTaintFlow(_, output)
@@ -132,13 +151,24 @@ predicate isSinkImpl(DataFlow::Node sink, Expr formatString) {
132151
}
133152

134153
module NonConstFlowConfig implements DataFlow::ConfigSig {
135-
predicate isSource(DataFlow::Node source) { isNonConst(source) }
154+
predicate isSource(DataFlow::Node source) {
155+
exists(Type t |
156+
isNonConst(source) and
157+
t = source.getType() and
158+
not cannotContainString(t)
159+
)
160+
}
136161

137162
predicate isSink(DataFlow::Node sink) { isSinkImpl(sink, _) }
138163

139164
predicate isBarrier(DataFlow::Node node) {
140165
// Ignore tracing non-const through array indices
141166
exists(ArrayExpr a | a.getArrayOffset() = node.asIndirectExpr())
167+
or
168+
exists(Type t |
169+
t = node.getType() and
170+
cannotContainString(t)
171+
)
142172
}
143173
}
144174

0 commit comments

Comments
 (0)