-
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
d23a3f8
7f6fd34
da99d36
3c4a386
eeb09ae
1130870
a57f803
9cb1c89
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Fixed an issue with the "Wrong type of arguments to formatting function" (`cpp/wrong-type-format-argument`) query causing false positive results in `build-mode: none` databases. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| | 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) | ||
| printf("%zx", s); // GOOD (we generally permit signedness changes) | ||
| printf("%d", s); // BAD [NOT DETECTED] | ||
| printf("%ld", s); // BAD [NOT DETECTED] | ||
| printf("%lld", s); // BAD [NOT DETECTED] | ||
| printf("%u", s); // BAD [NOT DETECTED] | ||
|
|
||
| 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
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? |
||
| printf("%u", &buffer[1023] - buffer); // BAD | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.