Skip to content

Commit 68a9154

Browse files
committed
Swift: Merge the two PrintfFormat implementations.
1 parent 5f42130 commit 68a9154

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

swift/ql/lib/codeql/swift/StringFormat.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,17 @@ class NsExceptionRaise extends FormattingFunction, Method {
9494
*/
9595
class PrintfFormat extends FormattingFunction, FreeFunction {
9696
int formatParamIndex;
97+
string modeChars;
9798

9899
PrintfFormat() {
99-
this.getShortName().matches("%printf%") and this.getParam(formatParamIndex).getName() = "format"
100+
modeChars = this.getShortName().regexpCapture("(.*)printf.*", 1) and
101+
this.getParam(formatParamIndex).getName() = "format"
100102
}
101103

102104
override int getFormatParameterIndex() { result = formatParamIndex }
105+
106+
/**
107+
* Holds if this `printf` is a variant of `sprintf`.
108+
*/
109+
predicate isSprintf() { modeChars.charAt(_) = "s" }
103110
}

swift/ql/lib/codeql/swift/security/CleartextLoggingExtensions.qll

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import swift
44
private import codeql.swift.dataflow.DataFlow
55
private import codeql.swift.dataflow.ExternalFlow
66
private import codeql.swift.security.SensitiveExprs
7+
private import codeql.swift.StringFormat
78

89
/** A data flow sink for cleartext logging of sensitive data vulnerabilities. */
910
abstract class CleartextLoggingSink extends DataFlow::Node { }
@@ -93,30 +94,6 @@ private class CleartextLoggingFieldAdditionalFlowStep extends CleartextLoggingAd
9394
}
9495
}
9596

96-
/**
97-
* A function that appears to be an imported C `printf` variant.
98-
* TODO: merge code with similar cases from the format string PR.
99-
*/
100-
private class PrintfFormat extends FreeFunction {
101-
int formatParamIndex;
102-
string modeChars;
103-
104-
PrintfFormat() {
105-
modeChars = this.getShortName().regexpCapture("(.*)printf.*", 1) and
106-
this.getParam(formatParamIndex).getName() = "format"
107-
}
108-
109-
/**
110-
* Gets the index of the format parameter.
111-
*/
112-
int getFormatParamIndex() { result = formatParamIndex }
113-
114-
/**
115-
* Holds if this `printf` is a variant of `sprintf`.
116-
*/
117-
predicate isSprintf() { modeChars.charAt(_) = "s" }
118-
}
119-
12097
/**
12198
* A sink that appears to be an imported C `printf` variant.
12299
*/
@@ -125,7 +102,7 @@ private class PrintfCleartextLoggingSink extends CleartextLoggingSink {
125102
exists(CallExpr ce, PrintfFormat f |
126103
ce.getStaticTarget() = f and
127104
(
128-
this.asExpr() = ce.getArgument(f.getFormatParamIndex()).getExpr() or
105+
this.asExpr() = ce.getArgument(f.getFormatParameterIndex()).getExpr() or
129106
this.asExpr() = ce.getArgument(f.getNumberOfParams() - 1).getExpr()
130107
) and
131108
not f.isSprintf()

0 commit comments

Comments
 (0)