-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 5 commits
d23a3f8
7f6fd34
da99d36
3c4a386
eeb09ae
1130870
a57f803
9cb1c89
34f405f
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:
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.
That's not quite true. Because the have nested function support, there is - somewhat unexpectedly - a function |
||
|
|
||
| printf("%zd", s); // GOOD | ||
| printf("%zi", s); // GOOD | ||
| printf("%zu", s); // GOOD (we generally permit signedness changes) [FALSE POSITIVE] | ||
| printf("%zx", s); // GOOD (we generally permit signedness changes) [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] | ||
|
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
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. I would expect at least one of these to be "GOOD" given that we mostly support 32-bit and 64-bit platforms, and as
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. Its bad because
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. How does this work in the case of a database without parse errors? Would both be flagged as 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. Yes they should be, except that we have several pieces of logic in the query designed to be permissive towards things that happen to be the same size - to reduce unwanted (if technically accurate) results. People do a lot of strange things with typedefs and conditional compilation for example that mean they may know more about what sizes a type could be than we do. You know what, I'm changing these two labels from
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.
👍 |
||
| printf("%u", &buffer[1023] - buffer); // BAD | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.