Skip to content

Commit cd3bccf

Browse files
committed
CPP: Fix FPs.
1 parent 1cf4449 commit cd3bccf

File tree

6 files changed

+27
-31
lines changed

6 files changed

+27
-31
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@ import semmle.code.cpp.commons.StringAnalysis
88
import semmle.code.cpp.models.interfaces.FormattingFunction
99
import semmle.code.cpp.models.implementations.Printf
1010

11+
class PrintfFormatAttribute extends FormatAttribute {
12+
PrintfFormatAttribute() {
13+
getArchetype() = "printf" or
14+
getArchetype() = "__printf__"
15+
}
16+
}
17+
1118
/**
1219
* A function that can be identified as a `printf` style formatting
1320
* function by its use of the GNU `format` attribute.
1421
*/
1522
class AttributeFormattingFunction extends FormattingFunction {
16-
FormatAttribute printf_attrib;
17-
1823
override string getCanonicalQLClass() { result = "AttributeFormattingFunction" }
1924

2025
AttributeFormattingFunction() {
21-
printf_attrib = getAnAttribute() and
22-
(
23-
printf_attrib.getArchetype() = "printf" or
24-
printf_attrib.getArchetype() = "__printf__"
25-
) and
26-
exists(printf_attrib.getFirstFormatArgIndex()) // exclude `vprintf` style format functions
26+
exists(PrintfFormatAttribute printf_attrib |
27+
printf_attrib = getAnAttribute() and
28+
exists(printf_attrib.getFirstFormatArgIndex()) // exclude `vprintf` style format functions
29+
)
2730
}
2831

29-
override int getFormatParameterIndex() { result = printf_attrib.getFormatIndex() }
32+
override int getFormatParameterIndex() {
33+
forex(PrintfFormatAttribute printf_attrib |
34+
printf_attrib = getAnAttribute() |
35+
result = printf_attrib.getFormatIndex()
36+
)
37+
}
3038
}
3139

3240
/**

cpp/ql/test/query-tests/Likely Bugs/Format/NonConstantFormat/NonConstantFormat.expected

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
| NonConstantFormat.c:30:10:30:16 | access to array | The format string argument to printf should be constant to prevent security issues and other potential errors. |
22
| NonConstantFormat.c:41:9:41:27 | call to any_random_function | The format string argument to printf should be constant to prevent security issues and other potential errors. |
3-
| a.c:15:37:15:45 | call to getString | The format string argument to myMultiplyDefinedPrintf should be constant to prevent security issues and other potential errors. |
4-
| a.c:16:27:16:35 | call to getString | The format string argument to myMultiplyDefinedPrintf should be constant to prevent security issues and other potential errors. |
5-
| a.c:17:38:17:46 | call to getString | The format string argument to myMultiplyDefinedPrintf2 should be constant to prevent security issues and other potential errors. |
6-
| a.c:18:28:18:36 | call to getString | The format string argument to myMultiplyDefinedPrintf2 should be constant to prevent security issues and other potential errors. |
7-
| b.c:12:37:12:45 | call to getString | The format string argument to myMultiplyDefinedPrintf should be constant to prevent security issues and other potential errors. |
8-
| b.c:13:27:13:35 | call to getString | The format string argument to myMultiplyDefinedPrintf should be constant to prevent security issues and other potential errors. |
9-
| b.c:14:38:14:46 | call to getString | The format string argument to myMultiplyDefinedPrintf2 should be constant to prevent security issues and other potential errors. |
10-
| b.c:15:28:15:36 | call to getString | The format string argument to myMultiplyDefinedPrintf2 should be constant to prevent security issues and other potential errors. |
113
| nested.cpp:21:23:21:26 | fmt0 | The format string argument to snprintf should be constant to prevent security issues and other potential errors. |
124
| nested.cpp:79:32:79:38 | call to get_fmt | The format string argument to diagnostic should be constant to prevent security issues and other potential errors. |
135
| nested.cpp:87:18:87:20 | fmt | The format string argument to diagnostic should be constant to prevent security issues and other potential errors. |

cpp/ql/test/query-tests/Likely Bugs/Format/NonConstantFormat/a.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ char *getString();
1212

1313
void test_custom_printf1()
1414
{
15-
myMultiplyDefinedPrintf("string", getString()); // GOOD [FALSE POSITIVE]
16-
myMultiplyDefinedPrintf(getString(), "string"); // BAD
17-
myMultiplyDefinedPrintf2("string", getString()); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
18-
myMultiplyDefinedPrintf2(getString(), "string"); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
15+
myMultiplyDefinedPrintf("string", getString()); // GOOD
16+
myMultiplyDefinedPrintf(getString(), "string"); // BAD [NOT DETECTED]
17+
myMultiplyDefinedPrintf2("string", getString()); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
18+
myMultiplyDefinedPrintf2(getString(), "string"); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
1919
}

cpp/ql/test/query-tests/Likely Bugs/Format/NonConstantFormat/b.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ char *getString();
99

1010
void test_custom_printf2(char *string)
1111
{
12-
myMultiplyDefinedPrintf("string", getString()); // GOOD [FALSE POSITIVE]
13-
myMultiplyDefinedPrintf(getString(), "string"); // BAD
14-
myMultiplyDefinedPrintf2("string", getString()); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
15-
myMultiplyDefinedPrintf2(getString(), "string"); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
12+
myMultiplyDefinedPrintf("string", getString()); // GOOD
13+
myMultiplyDefinedPrintf(getString(), "string"); // BAD [NOT DETECTED]
14+
myMultiplyDefinedPrintf2("string", getString()); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
15+
myMultiplyDefinedPrintf2(getString(), "string"); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
1616
}

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
| a.c:13:39:13:42 | 1.0 | This argument should be of type 'char *' but is of type 'double' |
2-
| a.c:13:39:13:42 | 1.0 | This argument should be of type 'int' but is of type 'double' |
3-
| a.c:15:40:15:43 | 1.0 | This argument should be of type 'char *' but is of type 'double' |
4-
| a.c:15:40:15:43 | 1.0 | This argument should be of type 'int' but is of type 'double' |
51
| format.h:16:59:16:61 | str | This argument should be of type 'int' but is of type 'char *' |
62
| format.h:16:64:16:64 | i | This argument should be of type 'double' but is of type 'int' |
73
| format.h:16:67:16:67 | d | This argument should be of type 'char *' but is of type 'double' |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/a.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void myMultiplyDefinedPrintf2(const char *format, const char *extraArg, ...);
1010
void test_custom_printf1()
1111
{
1212
myMultiplyDefinedPrintf("%i", "%s", 1); // GOOD
13-
myMultiplyDefinedPrintf("%i", "%s", 1.0f); // BAD
13+
myMultiplyDefinedPrintf("%i", "%s", 1.0f); // BAD [NOT DETECTED]
1414
myMultiplyDefinedPrintf2("%i", "%s", 1); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
15-
myMultiplyDefinedPrintf2("%i", "%s", 1.0f); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
15+
myMultiplyDefinedPrintf2("%i", "%s", 1.0f); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
1616
}

0 commit comments

Comments
 (0)