Skip to content

Commit b15db5d

Browse files
committed
CPP: Add a test case that we handle poorly.
1 parent 7f56be6 commit b15db5d

File tree

8 files changed

+48
-4
lines changed

8 files changed

+48
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
| printf1.h:73:19:73:22 | c_st | This argument should be of type 'ssize_t' but is of type 'unsigned long' |
1919
| printf1.h:74:19:74:22 | C_ST | This argument should be of type 'ssize_t' but is of type 'unsigned long' |
2020
| printf1.h:75:19:75:28 | sizeof(<expr>) | This argument should be of type 'ssize_t' but is of type 'unsigned long' |
21+
| printf1.h:82:23:82:35 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long' |
2122
| printf1.h:83:23:83:35 | ... - ... | This argument should be of type 'size_t' but is of type 'long' |
23+
| printf1.h:102:19:102:21 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long' |
2224
| real_world.h:61:21:61:22 | & ... | This argument should be of type 'int *' but is of type 'short *' |
2325
| real_world.h:62:22:62:23 | & ... | This argument should be of type 'short *' but is of type 'int *' |
2426
| real_world.h:63:22:63:24 | & ... | This argument should be of type 'short *' but is of type 'unsigned int *' |

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void g()
7979
char buf[100];
8080

8181
printf("%tu", ptr_a - ptr_b); // ok
82-
printf("%td", ptr_a - ptr_b); // ok
82+
printf("%td", ptr_a - ptr_b); // ok [FALSE POSITIVE]
8383
printf("%zu", ptr_a - ptr_b); // ok (dubious) [DETECTED ON LINUX ONLY]
8484
printf("%zd", ptr_a - ptr_b); // ok (dubious) [DETECTED ON MICROSOFT ONLY]
8585
}
@@ -92,3 +92,12 @@ void h(int i, struct some_type *j, int k)
9292
// going on.
9393
printf("%i %R %i", i, j, k); // GOOD (as far as we can tell)
9494
}
95+
96+
typedef long ptrdiff_t;
97+
98+
void fun1(unsigned char* a, unsigned char* b) {
99+
ptrdiff_t pdt;
100+
101+
printf("%td\n", pdt); // GOOD
102+
printf("%td\n", a-b); // GOOD [FALSE POSITIVE]
103+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
| printf1.h:73:19:73:22 | c_st | This argument should be of type 'ssize_t' but is of type 'unsigned long' |
1919
| printf1.h:74:19:74:22 | C_ST | This argument should be of type 'ssize_t' but is of type 'unsigned long' |
2020
| printf1.h:75:19:75:28 | sizeof(<expr>) | This argument should be of type 'ssize_t' but is of type 'unsigned long' |
21+
| printf1.h:82:23:82:35 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long' |
2122
| printf1.h:83:23:83:35 | ... - ... | This argument should be of type 'size_t' but is of type 'long' |
23+
| printf1.h:102:19:102:21 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long' |
2224
| real_world.h:61:21:61:22 | & ... | This argument should be of type 'int *' but is of type 'short *' |
2325
| real_world.h:62:22:62:23 | & ... | This argument should be of type 'short *' but is of type 'int *' |
2426
| real_world.h:63:22:63:24 | & ... | This argument should be of type 'short *' but is of type 'unsigned int *' |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_unsigned_chars/printf1.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void g()
7979
char buf[100];
8080

8181
printf("%tu", ptr_a - ptr_b); // ok
82-
printf("%td", ptr_a - ptr_b); // ok
82+
printf("%td", ptr_a - ptr_b); // ok [FALSE POSITIVE]
8383
printf("%zu", ptr_a - ptr_b); // ok (dubious) [DETECTED ON LINUX ONLY]
8484
printf("%zd", ptr_a - ptr_b); // ok (dubious) [DETECTED ON MICROSOFT ONLY]
8585
}
@@ -92,3 +92,12 @@ void h(int i, struct some_type *j, int k)
9292
// going on.
9393
printf("%i %R %i", i, j, k); // GOOD (as far as we can tell)
9494
}
95+
96+
typedef long ptrdiff_t;
97+
98+
void fun1(unsigned char* a, unsigned char* b) {
99+
ptrdiff_t pdt;
100+
101+
printf("%td\n", pdt); // GOOD
102+
printf("%td\n", a-b); // GOOD [FALSE POSITIVE]
103+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
| printf1.h:73:19:73:22 | c_st | This argument should be of type 'ssize_t' but is of type 'unsigned long long' |
1818
| printf1.h:74:19:74:22 | C_ST | This argument should be of type 'ssize_t' but is of type 'unsigned long long' |
1919
| printf1.h:75:19:75:28 | sizeof(<expr>) | This argument should be of type 'ssize_t' but is of type 'unsigned long long' |
20+
| printf1.h:82:23:82:35 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long long' |
2021
| printf1.h:84:23:84:35 | ... - ... | This argument should be of type 'ssize_t' but is of type 'long long' |
22+
| printf1.h:102:19:102:21 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long long' |
2123
| real_world.h:61:21:61:22 | & ... | This argument should be of type 'int *' but is of type 'short *' |
2224
| real_world.h:62:22:62:23 | & ... | This argument should be of type 'short *' but is of type 'int *' |
2325
| real_world.h:63:22:63:24 | & ... | This argument should be of type 'short *' but is of type 'unsigned int *' |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft/printf1.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void g()
7979
char buf[100];
8080

8181
printf("%tu", ptr_a - ptr_b); // ok
82-
printf("%td", ptr_a - ptr_b); // ok
82+
printf("%td", ptr_a - ptr_b); // ok [FALSE POSITIVE]
8383
printf("%zu", ptr_a - ptr_b); // ok (dubious) [DETECTED ON LINUX ONLY]
8484
printf("%zd", ptr_a - ptr_b); // ok (dubious) [DETECTED ON MICROSOFT ONLY]
8585
}
@@ -92,3 +92,12 @@ void h(int i, struct some_type *j, int k)
9292
// going on.
9393
printf("%i %R %i", i, j, k); // GOOD (as far as we can tell)
9494
}
95+
96+
typedef long long ptrdiff_t;
97+
98+
void fun1(unsigned char* a, unsigned char* b) {
99+
ptrdiff_t pdt;
100+
101+
printf("%td\n", pdt); // GOOD
102+
printf("%td\n", a-b); // GOOD [FALSE POSITIVE]
103+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
| printf1.h:73:19:73:22 | c_st | This argument should be of type 'ssize_t' but is of type 'unsigned long long' |
1818
| printf1.h:74:19:74:22 | C_ST | This argument should be of type 'ssize_t' but is of type 'unsigned long long' |
1919
| printf1.h:75:19:75:28 | sizeof(<expr>) | This argument should be of type 'ssize_t' but is of type 'unsigned long long' |
20+
| printf1.h:82:23:82:35 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long long' |
2021
| printf1.h:84:23:84:35 | ... - ... | This argument should be of type 'ssize_t' but is of type 'long long' |
22+
| printf1.h:102:19:102:21 | ... - ... | This argument should be of type 'ptrdiff_t' but is of type 'long long' |
2123
| real_world.h:61:21:61:22 | & ... | This argument should be of type 'int *' but is of type 'short *' |
2224
| real_world.h:62:22:62:23 | & ... | This argument should be of type 'short *' but is of type 'int *' |
2325
| real_world.h:63:22:63:24 | & ... | This argument should be of type 'short *' but is of type 'unsigned int *' |

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void g()
7979
char buf[100];
8080

8181
printf("%tu", ptr_a - ptr_b); // ok
82-
printf("%td", ptr_a - ptr_b); // ok
82+
printf("%td", ptr_a - ptr_b); // ok [FALSE POSITIVE]
8383
printf("%zu", ptr_a - ptr_b); // ok (dubious) [DETECTED ON LINUX ONLY]
8484
printf("%zd", ptr_a - ptr_b); // ok (dubious) [DETECTED ON MICROSOFT ONLY]
8585
}
@@ -92,3 +92,12 @@ void h(int i, struct some_type *j, int k)
9292
// going on.
9393
printf("%i %R %i", i, j, k); // GOOD (as far as we can tell)
9494
}
95+
96+
typedef long long ptrdiff_t;
97+
98+
void fun1(unsigned char* a, unsigned char* b) {
99+
ptrdiff_t pdt;
100+
101+
printf("%td\n", pdt); // GOOD
102+
printf("%td\n", a-b); // GOOD [FALSE POSITIVE]
103+
}

0 commit comments

Comments
 (0)