Skip to content

Commit 950b5f2

Browse files
authored
C++: Block flow out of 'gettext' in 'cpp/non-constant-format'. (#52)
1 parent fc509c2 commit 950b5f2

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import semmle.code.cpp.ir.dataflow.TaintTracking
19+
import semmle.code.cpp.models.implementations.GetText
1920
import semmle.code.cpp.commons.Printf
2021

2122
// For the following `...gettext` functions, we assume that
@@ -26,30 +27,21 @@ predicate whitelistFunction(Function f, int arg) {
2627
// basic variations of gettext
2728
f.getName() = "_" and arg = 0
2829
or
29-
f.getName() = "gettext" and arg = 0
30-
or
31-
f.getName() = "dgettext" and arg = 1
32-
or
33-
f.getName() = "dcgettext" and arg = 1
34-
or
35-
// plural variations of gettext that take one format string for singular and another for plural form
36-
f.getName() = "ngettext" and
37-
(arg = 0 or arg = 1)
38-
or
39-
f.getName() = "dngettext" and
40-
(arg = 1 or arg = 2)
41-
or
42-
f.getName() = "dcngettext" and
43-
(arg = 1 or arg = 2)
30+
exists(FunctionInput input |
31+
f.(GetTextFunction).hasDataFlow(input, _) and
32+
input.isParameterDeref(arg)
33+
)
4434
}
4535

46-
// we assume that ALL uses of the `_` macro
36+
// we assume that ALL uses of the `_` macro (and calls to `gettext`)
4737
// return constant string literals
4838
predicate underscoreMacro(Expr e) {
4939
exists(MacroInvocation mi |
5040
mi.getMacroName() = "_" and
5141
mi.getExpr() = e
5242
)
43+
or
44+
e = any(GetTextFunction gettext).getACallToThisFunction()
5345
}
5446

5547
/**

0 commit comments

Comments
 (0)