-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathStringFormat.qll
110 lines (91 loc) · 2.9 KB
/
StringFormat.qll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Provides classes and predicates for reasoning about string formatting.
*/
import swift
/**
* A function that takes a `printf` style format argument.
*/
abstract class FormattingFunction extends Function {
/**
* Gets the position of the format argument.
*/
abstract int getFormatParameterIndex();
}
/**
* A call to a function that takes a `printf` style format argument.
*/
class FormattingFunctionCall extends CallExpr {
FormattingFunction target;
FormattingFunctionCall() { target = this.getStaticTarget() }
/**
* Gets the format expression used in this call.
*/
Expr getFormat() { result = this.getArgument(target.getFormatParameterIndex()).getExpr() }
}
/**
* An initializer for `String`, `NSString` or `NSMutableString` that takes a
* `printf` style format argument.
*/
class StringInitWithFormat extends FormattingFunction, Method {
StringInitWithFormat() {
exists(string fName |
this.hasQualifiedName(["String", "NSString", "NSMutableString"], fName) and
fName.matches("init(format:%")
)
}
override int getFormatParameterIndex() { result = 0 }
}
/**
* The `localizedStringWithFormat` method of `String`, `NSString` and `NSMutableString`.
*/
class LocalizedStringWithFormat extends FormattingFunction, Method {
LocalizedStringWithFormat() {
this.hasQualifiedName(["String", "NSString", "NSMutableString"],
"localizedStringWithFormat(_:_:)")
}
override int getFormatParameterIndex() { result = 0 }
}
/**
* A method that appends a formatted string.
*/
class StringMethodWithFormat extends FormattingFunction, Method {
StringMethodWithFormat() {
this.hasQualifiedName("NSMutableString", "appendFormat(_:_:)")
or
this.hasQualifiedName("StringProtocol", "appendingFormat(_:_:)")
}
override int getFormatParameterIndex() { result = 0 }
}
/**
* The functions `NSLog` and `NSLogv`.
*/
class NsLog extends FormattingFunction, FreeFunction {
NsLog() { this.getName() = ["NSLog(_:_:)", "NSLogv(_:_:)"] }
override int getFormatParameterIndex() { result = 0 }
}
/**
* The `NSException.init` and `NSException.raise` methods.
*/
class NsExceptionRaise extends FormattingFunction, Method {
NsExceptionRaise() {
this.hasQualifiedName("NSException", "init(name:reason:userInfo:)") or
this.hasQualifiedName("NSException", "raise(_:format:arguments:)")
}
override int getFormatParameterIndex() { result = 1 }
}
/**
* A function that appears to be an imported C `printf` variant.
*/
class PrintfFormat extends FormattingFunction, FreeFunction {
int formatParamIndex;
string modeChars;
PrintfFormat() {
modeChars = this.getShortName().regexpCapture("(.*)printf.*", 1) and
this.getParam(formatParamIndex).getName() = "format"
}
override int getFormatParameterIndex() { result = formatParamIndex }
/**
* Holds if this `printf` is a variant of `sprintf`.
*/
predicate isSprintf() { modeChars.charAt(_) = "s" }
}