-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C++: Add some test cases for cpp/wrong-type-format-argument #21421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| | second.cpp:13:19:13:19 | s | This format specifier for type 'size_t' does not match the argument type '..(*)(..)'. | | ||
| | second.cpp:14:19:14:19 | s | This format specifier for type 'size_t' does not match the argument type '..(*)(..)'. | | ||
| | second.cpp:15:18:15:18 | s | This format specifier for type 'int' does not match the argument type '..(*)(..)'. | | ||
| | second.cpp:16:19:16:19 | s | This format specifier for type 'long' does not match the argument type '..(*)(..)'. | | ||
| | second.cpp:17:20:17:20 | s | This format specifier for type 'long long' does not match the argument type '..(*)(..)'. | | ||
| | second.cpp:18:18:18:18 | s | This format specifier for type 'unsigned int' does not match the argument type '..(*)(..)'. | | ||
| | second.cpp:26:18:26:39 | ... - ... | This format specifier for type 'int' does not match the argument type 'long'. | | ||
| | second.cpp:29:18:29:39 | ... - ... | This format specifier for type 'unsigned int' does not match the argument type 'long'. | | ||
| | tests.c:7:18:7:18 | 1 | This format specifier for type 'char *' does not match the argument type 'int'. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
|
|
||
| // defines type size_t plausibly | ||
| typedef unsigned long size_t; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // semmle-extractor-options: --expect_errors | ||
|
|
||
| int printf(const char * format, ...); | ||
|
|
||
| // defines type `myFunctionPointerType`, referencing `size_t` | ||
| typedef size_t (*myFunctionPointerType) (); | ||
|
|
||
| void test_size_t() { | ||
| size_t s = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should add a comment on what our frontend thinks
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two But which type does the variable "s" have? ... it looks like there is no If I take the definition of So I think we have two paths available to fixing this:
|
||
|
|
||
| printf("%zd", s); // GOOD | ||
| printf("%zi", s); // GOOD | ||
| printf("%zu", s); // GOOD [FALSE POSITIVE] | ||
| printf("%zx", s); // GOOD [FALSE POSITIVE] | ||
| printf("%d", s); // BAD | ||
| printf("%ld", s); // BAD | ||
| printf("%lld", s); // BAD | ||
| printf("%u", s); // BAD | ||
|
|
||
| char buffer[1024]; | ||
|
|
||
| printf("%zd", &buffer[1023] - buffer); // GOOD | ||
| printf("%zi", &buffer[1023] - buffer); // GOOD | ||
| printf("%zu", &buffer[1023] - buffer); // GOOD | ||
| printf("%zx", &buffer[1023] - buffer); // GOOD | ||
| printf("%d", &buffer[1023] - buffer); // BAD | ||
| printf("%ld", &buffer[1023] - buffer); // BAD [NOT DETECTED] | ||
| printf("%lld", &buffer[1023] - buffer); // BAD [NOT DETECTED] | ||
|
Comment on lines
+27
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these bad?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the pointer type is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ok. I think I misunderstand why we're running into problems with the query. There seem to be at least three reasons going by the tests you're adding here:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The primary issue is the results where the query claims the argument has a function type |
||
| printf("%u", &buffer[1023] - buffer); // BAD | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this file at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this line we lose two of the results, and they're the two closest to the real world cases I was looking at:
Though we would retain the other results involving function pointer types, for example:
What we can't do is move the definition of
size_tintosecond.cpp. That makes the entire phenomena disappear.TL;DR: I'd prefer to keep this as it better models real world cases; but it's not strictly necessary to reproduce an issue.