Skip to content

Commit 7c00477

Browse files
committed
C++: Combine getOutputParameterIndex and isOutputStream.
1 parent c9c159a commit 7c00477

File tree

6 files changed

+29
-31
lines changed

6 files changed

+29
-31
lines changed

cpp/ql/src/semmle/code/cpp/commons/Printf.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ class UserDefinedFormattingFunction extends FormattingFunction {
128128

129129
override int getFormatParameterIndex() { callsVariadicFormatter(this, _, result, _) }
130130

131-
override int getOutputParameterIndex() {
132-
callsVariadicFormatter(this, _, _, result) and not result = -1
131+
override int getOutputParameterIndex(boolean isStream) {
132+
callsVariadicFormatter(this, "f", _, result) and isStream = true
133+
or
134+
callsVariadicFormatter(this, "s", _, result) and isStream = false
133135
}
134136

135137
override predicate isOutputGlobal() { callsVariadicFormatter(this, "", _, _) }
136-
137-
override predicate isOutputStream() { callsVariadicFormatter(this, "f", _, _) }
138138
}
139139

140140
/**

cpp/ql/src/semmle/code/cpp/models/implementations/Printf.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ private class Fprintf extends FormattingFunction {
5858

5959
deprecated override predicate isWideCharDefault() { hasGlobalOrStdName("fwprintf") }
6060

61-
override int getOutputParameterIndex() { result = 0 }
62-
63-
override predicate isOutputStream() { any() }
61+
override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = true }
6462
}
6563

6664
/**
@@ -113,7 +111,9 @@ private class Sprintf extends FormattingFunction {
113111
result = 1
114112
}
115113

116-
override int getOutputParameterIndex() { not hasGlobalName("g_strdup_printf") and result = 0 }
114+
override int getOutputParameterIndex(boolean isStream) {
115+
not hasGlobalName("g_strdup_printf") and result = 0 and isStream = false
116+
}
117117

118118
override int getFirstFormatArgumentIndex() {
119119
if hasGlobalName("__builtin___sprintf_chk")
@@ -168,7 +168,7 @@ private class SnprintfImpl extends Snprintf {
168168
.getSize() > 1
169169
}
170170

171-
override int getOutputParameterIndex() { result = 0 }
171+
override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = false }
172172

173173
override int getFirstFormatArgumentIndex() {
174174
exists(string name |
@@ -228,7 +228,7 @@ private class StringCchPrintf extends FormattingFunction {
228228
.getSize() > 1
229229
}
230230

231-
override int getOutputParameterIndex() { result = 0 }
231+
override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = false }
232232

233233
override int getSizeParameterIndex() { result = 1 }
234234
}

cpp/ql/src/semmle/code/cpp/models/interfaces/FormattingFunction.qll

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,25 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
109109
}
110110

111111
/**
112-
* Gets the position at which the output parameter, if any, occurs. This may
113-
* be a buffer that characters are written to if this function behaves like
114-
* `sprintf`. Alternatively it may be a stream that is used for output if
115-
* this function behaves like `fprintf` (see `isOutputStream`).
112+
* Gets the position at which the output parameter, if any, occurs. If
113+
* `isStream` is `true`, the output parameter is a stream (that is, this
114+
* function behaves like `fprintf`). If `isStream` is `false`, the output
115+
* parameter is a buffer (that is, this function behaves like `sprintf`).
116116
*/
117-
int getOutputParameterIndex() { none() }
117+
int getOutputParameterIndex(boolean isStream) { none() }
118118

119119
/**
120-
* Holds if this function outputs to a global stream such as standard output,
121-
* standard error or a system log. For example `printf`.
120+
* Gets the position at which the output parameter, if any, occurs.
121+
*
122+
* DEPRECATED: use `getOutputParameterIndex(boolean isStream)` instead.
122123
*/
123-
predicate isOutputGlobal() { none() }
124+
deprecated int getOutputParameterIndex() { result = getOutputParameterIndex(_) }
124125

125126
/**
126-
* Holds if this function outputs to the stream indicated by
127-
* `getOutputParameterIndex()`, that is, this function behaves like `fprintf`.
127+
* Holds if this function outputs to a global stream such as standard output,
128+
* standard error or a system log. For example `printf`.
128129
*/
129-
predicate isOutputStream() { none() }
130+
predicate isOutputGlobal() { none() }
130131

131132
/**
132133
* Gets the position of the first format argument, corresponding with
@@ -156,20 +157,18 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
156157
}
157158

158159
override predicate hasArrayWithVariableSize(int bufParam, int countParam) {
159-
bufParam = getOutputParameterIndex() and
160+
bufParam = getOutputParameterIndex(false) and
160161
countParam = getSizeParameterIndex()
161162
}
162163

163164
override predicate hasArrayWithUnknownSize(int bufParam) {
164-
bufParam = getOutputParameterIndex() and
165+
bufParam = getOutputParameterIndex(false) and
165166
not exists(getSizeParameterIndex())
166167
}
167168

168169
override predicate hasArrayInput(int bufParam) { bufParam = getFormatParameterIndex() }
169170

170-
override predicate hasArrayOutput(int bufParam) {
171-
bufParam = getOutputParameterIndex() and not isOutputStream()
172-
}
171+
override predicate hasArrayOutput(int bufParam) { bufParam = getOutputParameterIndex(false) }
173172

174173
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
175174
exists(int arg |
@@ -178,7 +177,7 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
178177
arg >= getFirstFormatArgumentIndex()
179178
) and
180179
input.isParameterDeref(arg) and
181-
output.isParameterDeref(getOutputParameterIndex())
180+
output.isParameterDeref(getOutputParameterIndex(_))
182181
)
183182
}
184183
}

cpp/ql/src/semmle/code/cpp/security/BufferWrite.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class SprintfBW extends BufferWriteCall {
229229
result = this.(FormattingFunctionCall).getFormatArgument(_)
230230
}
231231

232-
override Expr getDest() { result = getArgument(f.getOutputParameterIndex()) }
232+
override Expr getDest() { result = getArgument(f.getOutputParameterIndex(false)) }
233233

234234
override int getMaxData() {
235235
exists(FormatLiteral fl |

cpp/ql/src/semmle/code/cpp/security/FileWrite.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ private predicate fileWrite(Call write, Expr source, Expr dest) {
143143
)
144144
or
145145
// fprintf
146-
f.(FormattingFunction).isOutputStream() and
147146
s >= f.(FormattingFunction).getFormatParameterIndex() and
148-
d = f.(FormattingFunction).getOutputParameterIndex()
147+
d = f.(FormattingFunction).getOutputParameterIndex(true)
149148
)
150149
or
151150
// file stream using '<<', 'put' or 'write'

cpp/ql/src/semmle/code/cpp/security/TaintTrackingImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ private predicate copyValueBetweenArguments(Function f, int sourceArg, int destA
478478
or
479479
exists(FormattingFunction ff | ff = f |
480480
sourceArg in [ff.getFormatParameterIndex() .. maxArgIndex(f)] and
481-
destArg = ff.getOutputParameterIndex()
481+
destArg = ff.getOutputParameterIndex(false)
482482
)
483483
}
484484

0 commit comments

Comments
 (0)