Skip to content

Commit 9c6a0ff

Browse files
authored
Merge pull request github#1979 from nickrolfe/wrong_type_uninstantiated
C++: ignore uninstantiated templates in WrongTypeFormatArguments.ql
2 parents 0387177 + 56f4f86 commit 9c6a0ff

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ where
157157
formatOtherArgType(ffc, n, expected, arg, actual) and
158158
not actual.getUnspecifiedType().(IntegralType).getSize() = sizeof_IntType()
159159
) and
160-
not arg.isAffectedByMacro()
160+
not arg.isAffectedByMacro() and
161+
not arg.isFromUninstantiatedTemplate(_)
161162
select arg,
162163
"This argument should be of type '" + expected.getName() + "' but is of type '" +
163164
actual.getUnspecifiedType().getName() + "'"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| format.h:16:59:16:61 | str | This argument should be of type 'int' but is of type 'char *' |
22
| format.h:16:64:16:64 | i | This argument should be of type 'double' but is of type 'int' |
33
| format.h:16:67:16:67 | d | This argument should be of type 'char *' but is of type 'double' |
4+
| linux.cpp:15:24:15:41 | call to get_template_value | This argument should be of type 'int' but is of type 'long' |
45
| linux_c.c:11:15:11:18 | str3 | This argument should be of type 'char *' but is of type 'short *' |
56
| pri_macros.h:15:35:15:40 | my_u64 | This argument should be of type 'unsigned int' but is of type 'unsigned long long' |
67
| printf1.h:12:27:12:27 | i | This argument should be of type 'double' but is of type 'int' |

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,23 @@ typedef unsigned long size_t;
22
typedef long ssize_t;
33

44
#include "common.h"
5+
6+
template <typename T>
7+
struct S {
8+
int get_int();
9+
T get_template_value();
10+
};
11+
12+
template <typename U>
13+
void template_func_calling_printf(S<U> &obj) {
14+
::printf("%d\n", obj.get_int());
15+
::printf("%d\n", obj.get_template_value());
16+
}
17+
18+
void instantiate() {
19+
S<int> s_int;
20+
S<long> s_long;
21+
22+
template_func_calling_printf(s_int); // ok
23+
template_func_calling_printf(s_long); // not ok (long -> int)
24+
}

0 commit comments

Comments
 (0)