Skip to content

Commit c3d275d

Browse files
committed
C++: Clean up SprintfBW and comment Sprintf.
1 parent c681e69 commit c3d275d

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,19 @@ class Sprintf extends FormattingFunction {
6666
Sprintf() {
6767
this instanceof TopLevelFunction and
6868
(
69+
// sprintf(dst, format, args...)
6970
hasGlobalOrStdName("sprintf") or
71+
// _sprintf_l(dst, format, locale, args...)
7072
hasGlobalName("_sprintf_l") or
73+
// __swprintf_l(dst, format, locale, args...)
7174
hasGlobalName("__swprintf_l") or
75+
// wsprintf(dst, format, args...)
7276
hasGlobalOrStdName("wsprintf") or
77+
// g_strdup_printf(format, ...)
7378
hasGlobalName("g_strdup_printf") or
79+
// g_sprintf(dst, format, ...)
7480
hasGlobalName("g_sprintf") or
81+
// __builtin___sprintf_chk(dst, flag, os, format, ...)
7582
hasGlobalName("__builtin___sprintf_chk")
7683
) and
7784
not exists(getDefinition().getFile().getRelativePath())

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,10 @@ class StrCatBW extends BufferWriteCall {
181181
* A call to a variant of `sprintf`.
182182
*/
183183
class SprintfBW extends BufferWriteCall {
184+
FormattingFunction f;
185+
184186
SprintfBW() {
185-
exists(TopLevelFunction fn, string name | fn = getTarget() and name = fn.getName() |
187+
exists(string name | f = getTarget().(TopLevelFunction) and name = f.getName() |
186188
/*
187189
* C sprintf variants:
188190
*/
@@ -218,10 +220,7 @@ class SprintfBW extends BufferWriteCall {
218220
}
219221

220222
override Type getBufferType() {
221-
exists(FormattingFunction f |
222-
f = this.getTarget() and
223-
result = f.getParameter(f.getFormatParameterIndex()).getUnspecifiedType()
224-
)
223+
result = f.getParameter(f.getFormatParameterIndex()).getUnspecifiedType()
225224
}
226225

227226
override Expr getASource() {
@@ -230,7 +229,7 @@ class SprintfBW extends BufferWriteCall {
230229
result = this.(FormattingFunctionCall).getFormatArgument(_)
231230
}
232231

233-
override Expr getDest() { result = getArgument(0) }
232+
override Expr getDest() { result = getArgument(f.getOutputParameterIndex()) }
234233

235234
override int getMaxData() {
236235
exists(FormatLiteral fl |

0 commit comments

Comments
 (0)