Skip to content

Commit a749b5b

Browse files
committed
CPP: Improve WrongTypeFormatArguments logic when there is more than one possible expected argument type.
1 parent ac277ad commit a749b5b

File tree

8 files changed

+14
-7
lines changed

8 files changed

+14
-7
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ private predicate formattingFunctionCallExpectedType(FormattingFunctionCall ffc,
2525
ffc.getTarget() = f and
2626
f.getFormatParameterIndex() = i and
2727
ffc.getArgument(i) = fl and
28-
fl.getConversionType(pos) = expected and
29-
count(fl.getConversionType(pos)) = 1
28+
fl.getConversionType(pos) = expected
3029
)
3130
}
3231

@@ -143,7 +142,10 @@ from FormattingFunctionCall ffc, int n, Expr arg, Type expected, Type actual
143142
where (
144143
(
145144
formatArgType(ffc, n, expected, arg, actual) and
146-
not trivialConversion(expected.getUnspecifiedType(), actual.getUnspecifiedType())
145+
not exists(Type anyExpected |
146+
formatArgType(ffc, n, anyExpected, arg, actual) and
147+
trivialConversion(anyExpected.getUnspecifiedType(), actual.getUnspecifiedType())
148+
)
147149
)
148150
or
149151
(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| tests.cpp:18:15:18:22 | Hello | This argument should be of type 'char *' but is of type 'char16_t *' |
22
| tests.cpp:19:15:19:22 | Hello | This argument should be of type 'char *' but is of type 'wchar_t *' |
3+
| tests.cpp:21:15:21:21 | Hello | This argument should be of type 'char16_t *' but is of type 'char *' |
4+
| tests.cpp:21:15:21:21 | Hello | This argument should be of type 'wchar_t *' but is of type 'char *' |
35
| tests.cpp:26:17:26:24 | Hello | This argument should be of type 'char *' but is of type 'char16_t *' |
46
| tests.cpp:27:17:27:24 | Hello | This argument should be of type 'char *' but is of type 'wchar_t *' |
57
| tests.cpp:29:17:29:23 | Hello | This argument should be of type 'wchar_t *' but is of type 'char *' |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void tests() {
1818
printf("%s", u"Hello"); // BAD: expecting char
1919
printf("%s", L"Hello"); // BAD: expecting char
2020

21-
printf("%S", "Hello"); // BAD: expecting wchar_t or char16_t [NOT DETECTED]
21+
printf("%S", "Hello"); // BAD: expecting wchar_t or char16_t
2222
printf("%S", u"Hello"); // GOOD
2323
printf("%S", L"Hello"); // GOOD
2424

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| tests_32.cpp:14:16:14:23 | void_ptr | This argument should be of type 'long' but is of type 'void *' |
2+
| tests_32.cpp:15:15:15:15 | l | This argument should be of type 'void *' but is of type 'long' |
23
| tests_64.cpp:14:16:14:23 | void_ptr | This argument should be of type 'long' but is of type 'void *' |
4+
| tests_64.cpp:15:15:15:15 | l | This argument should be of type 'void *' but is of type 'long' |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_mixed_word_size/tests_32.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ void test_32()
1212

1313
printf("%li", l); // GOOD
1414
printf("%li", void_ptr); // BAD
15-
printf("%p", l); // BAD [NOT DETECTED]
15+
printf("%p", l); // BAD
1616
printf("%p", void_ptr); // GOOD
1717
}

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_mixed_word_size/tests_64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ void test_64()
1212

1313
printf("%li", l); // GOOD
1414
printf("%li", void_ptr); // BAD
15-
printf("%p", l); // BAD [NOT DETECTED]
15+
printf("%p", l); // BAD
1616
printf("%p", void_ptr); // GOOD
1717
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@
3232
| real_world.h:63:22:63:24 | & ... | This argument should be of type 'short *' but is of type 'unsigned int *' |
3333
| real_world.h:64:22:64:24 | & ... | This argument should be of type 'short *' but is of type 'signed int *' |
3434
| wide_string.h:25:18:25:20 | c | This argument should be of type 'char' but is of type 'char *' |
35+
| wide_string.h:29:19:29:22 | c | This argument should be of type 'wchar_t' but is of type 'unsigned short *' |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft_no_wchar/wide_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ void test_wchar4(char c, const char cc, wchar_t wc, const wchar_t wcc) {
2626
printf("%wc", wc); // GOOD
2727
printf("%wc", wcc); // GOOD
2828
printf("%wc", L'c'); // GOOD
29-
printf("%wc", L"c"); // BAD [NOT DETECTED]
29+
printf("%wc", L"c"); // BAD
3030
}

0 commit comments

Comments
 (0)