Skip to content

Commit cb0cc8d

Browse files
authored
Merge pull request github#7625 from geoffw0/nullterm4
C++: Fix some code duplication.
2 parents b16b027 + d475101 commit cb0cc8d

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

cpp/ql/lib/semmle/code/cpp/commons/NullTermination.qll

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ predicate functionArgumentMustBeNullTerminated(Function f, int i) {
101101
f instanceof StrcatFunction and i = 0
102102
}
103103

104+
/**
105+
* Holds if `arg` is a string format argument to a formatting function call
106+
* `ffc`.
107+
*/
108+
predicate formatArgumentMustBeNullTerminated(FormattingFunctionCall ffc, Expr arg) {
109+
// String argument to a formatting function (such as `printf`)
110+
exists(int n, FormatLiteral fl |
111+
ffc.getConversionArgument(n) = arg and
112+
fl = ffc.getFormat() and
113+
fl.getConversionType(n) instanceof PointerType and // `%s`, `%ws` etc
114+
not fl.getConversionType(n) instanceof VoidPointerType and // exclude: `%p`
115+
not fl.hasPrecision(n) // exclude: `%.*s`
116+
)
117+
}
118+
104119
/**
105120
* Holds if `va` is a variable access where the contents must be null terminated.
106121
*/
@@ -113,13 +128,7 @@ predicate variableMustBeNullTerminated(VariableAccess va) {
113128
)
114129
or
115130
// String argument to a formatting function (such as `printf`)
116-
exists(int n, FormatLiteral fl |
117-
fc.(FormattingFunctionCall).getConversionArgument(n) = va and
118-
fl = fc.(FormattingFunctionCall).getFormat() and
119-
fl.getConversionType(n) instanceof PointerType and // `%s`, `%ws` etc
120-
not fl.getConversionType(n) instanceof VoidPointerType and // exclude: `%p`
121-
not fl.hasPrecision(n) // exclude: `%.*s`
122-
)
131+
formatArgumentMustBeNullTerminated(fc, va)
123132
or
124133
// Call to a wrapper function that requires null termination
125134
// (not itself adding a null terminator)

cpp/ql/src/Security/CWE/CWE-131/NoSpaceForZeroTerminator.ql

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import cpp
1919
import semmle.code.cpp.dataflow.DataFlow
2020
import semmle.code.cpp.models.interfaces.ArrayFunction
2121
import semmle.code.cpp.models.interfaces.Allocation
22+
import semmle.code.cpp.commons.NullTermination
2223

2324
predicate terminationProblem(AllocationExpr malloc, string msg) {
2425
// malloc(strlen(...))
@@ -35,13 +36,7 @@ predicate terminationProblem(AllocationExpr malloc, string msg) {
3536
af.hasArrayWithUnknownSize(arg)
3637
or
3738
// flows into string argument to a formatting function (such as `printf`)
38-
exists(int n, FormatLiteral fl |
39-
fc.getArgument(arg) = fc.(FormattingFunctionCall).getConversionArgument(n) and
40-
fl = fc.(FormattingFunctionCall).getFormat() and
41-
fl.getConversionType(n) instanceof PointerType and // `%s`, `%ws` etc
42-
not fl.getConversionType(n) instanceof VoidPointerType and // exclude: `%p`
43-
not fl.hasPrecision(n) // exclude: `%.*s`
44-
)
39+
formatArgumentMustBeNullTerminated(fc, fc.getArgument(arg))
4540
)
4641
) and
4742
msg = "This allocation does not include space to null-terminate the string."

0 commit comments

Comments
 (0)