Skip to content

Commit 1d46971

Browse files
committed
C++: Add an ArrayFunction model to FormattingFunction.
1 parent 06f5720 commit 1d46971

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* `FormattingFunction` to match the flow within that function.
77
*/
88

9-
import semmle.code.cpp.Function
9+
import semmle.code.cpp.models.interfaces.ArrayFunction
1010

1111
private Type stripTopLevelSpecifiersOnly(Type t) {
1212
result = stripTopLevelSpecifiersOnly(t.(SpecifiedType).getBaseType())
@@ -39,7 +39,7 @@ private Type getAFormatterWideTypeOrDefault() {
3939
/**
4040
* A standard library function that uses a `printf`-like formatting string.
4141
*/
42-
abstract class FormattingFunction extends Function {
42+
abstract class FormattingFunction extends ArrayFunction {
4343
/** Gets the position at which the format parameter occurs. */
4444
abstract int getFormatParameterIndex();
4545

@@ -133,4 +133,26 @@ abstract class FormattingFunction extends Function {
133133
* Gets the position of the buffer size argument, if any.
134134
*/
135135
int getSizeParameterIndex() { none() }
136+
137+
override predicate hasArrayWithNullTerminator(int bufParam) {
138+
bufParam = getFormatParameterIndex()
139+
}
140+
141+
override predicate hasArrayWithVariableSize(int bufParam, int countParam) {
142+
bufParam = getOutputParameterIndex() and
143+
countParam = getSizeParameterIndex()
144+
}
145+
146+
override predicate hasArrayWithUnknownSize(int bufParam) {
147+
bufParam = getOutputParameterIndex() and
148+
not exists(getSizeParameterIndex())
149+
}
150+
151+
predicate hasArrayInput(int bufParam) {
152+
bufParam = getFormatParameterIndex()
153+
}
154+
155+
predicate hasArrayOutput(int bufParam) {
156+
bufParam = getOutputParameterIndex()
157+
}
136158
}

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/NoSpaceForZeroTerminator.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| test.c:32:20:32:25 | call to malloc | This allocation does not include space to null-terminate the string. |
55
| test.c:49:20:49:25 | call to malloc | This allocation does not include space to null-terminate the string. |
66
| test.cpp:24:35:24:40 | call to malloc | This allocation does not include space to null-terminate the string. |
7+
| test.cpp:45:28:45:33 | call to malloc | This allocation does not include space to null-terminate the string. |
78
| test.cpp:63:28:63:33 | call to malloc | This allocation does not include space to null-terminate the string. |
89
| test.cpp:71:28:71:33 | call to malloc | This allocation does not include space to null-terminate the string. |
910
| test.cpp:79:28:79:33 | call to malloc | This allocation does not include space to null-terminate the string. |

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void good1(wchar_t *wstr) {
4141
}
4242

4343
void bad3(char *str) {
44-
// BAD -- zero-termination proved by sprintf (as destination) [NOT DETECTED]
44+
// BAD -- zero-termination proved by sprintf (as destination)
4545
char *buffer = (char *)malloc(strlen(str));
4646
sprintf(buffer, "%s", str);
4747
free(buffer);

0 commit comments

Comments
 (0)