Skip to content

Commit bc98a80

Browse files
Merge pull request #1 from jbj/NonConstantFormat-ArrayExpr
C++: NonConstantFormat taint only for string types
2 parents 88a39d9 + cace411 commit bc98a80

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ predicate underscoreMacro(Expr e) {
5151
)
5252
}
5353

54+
/**
55+
* Holds if `t` cannot hold a character array, directly or indirectly.
56+
*/
57+
predicate cannotContainString(Type t) {
58+
t.getUnspecifiedType() instanceof BuiltInType
59+
or
60+
t.getUnspecifiedType() instanceof IntegralOrEnumType
61+
}
62+
5463
predicate isNonConst(DataFlow::Node node) {
5564
exists(Expr e | e = node.asExpr() |
5665
exists(FunctionCall fc | fc = e.(FunctionCall) |
@@ -99,16 +108,26 @@ predicate isNonConst(DataFlow::Node node) {
99108
node instanceof DataFlow::DefinitionByReferenceNode
100109
}
101110

111+
pragma[noinline]
112+
predicate isSanitizerNode(DataFlow::Node node) {
113+
underscoreMacro(node.asExpr())
114+
or
115+
cannotContainString(node.getType())
116+
}
117+
102118
class NonConstFlow extends TaintTracking::Configuration {
103119
NonConstFlow() { this = "NonConstFlow" }
104120

105-
override predicate isSource(DataFlow::Node source) { isNonConst(source) }
121+
override predicate isSource(DataFlow::Node source) {
122+
isNonConst(source) and
123+
not cannotContainString(source.getType())
124+
}
106125

107126
override predicate isSink(DataFlow::Node sink) {
108127
exists(FormattingFunctionCall fc | sink.asExpr() = fc.getArgument(fc.getFormatParameterIndex()))
109128
}
110129

111-
override predicate isSanitizer(DataFlow::Node node) { underscoreMacro(node.asExpr()) }
130+
override predicate isSanitizer(DataFlow::Node node) { isSanitizerNode(node) }
112131
}
113132

114133
from FormattingFunctionCall call, Expr formatString

cpp/ql/test/query-tests/Likely Bugs/Format/NonConstantFormat/test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,11 @@ void another_func(void) {
133133
printf("Hello, World\n"); // GOOD
134134
printf(gettext("Hello, World\n")); // GOOD
135135
}
136+
137+
void set_value_of(int *i);
138+
139+
void print_ith_message() {
140+
int i;
141+
set_value_of(&i);
142+
printf(messages[i], 1U); // GOOD
143+
}

0 commit comments

Comments
 (0)