-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #901 Improve out-of-bounds check to detect error with sprintf() #8473
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 |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| #include "utils.h" | ||
| #include "valueflow.h" | ||
| #include "vfvalue.h" | ||
| #include "vf_common.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdlib> | ||
|
|
@@ -83,7 +84,7 @@ | |
| return (op->valueType() && op->valueType()->pointer) ? op : tok; | ||
| } | ||
|
|
||
| static int getMinFormatStringOutputLength(const std::vector<const Token*> ¶meters, nonneg int formatStringArgNr) | ||
| static int getMinFormatStringOutputLength(const std::vector<const Token*> ¶meters, nonneg int formatStringArgNr, const Settings& settings) | ||
| { | ||
| if (formatStringArgNr <= 0 || formatStringArgNr > parameters.size()) | ||
| return 0; | ||
|
|
@@ -138,8 +139,8 @@ | |
| break; | ||
| case 's': | ||
| parameterLength = 0; | ||
| if (inputArgNr < parameters.size() && parameters[inputArgNr]->tokType() == Token::eString) | ||
| parameterLength = Token::getStrLength(parameters[inputArgNr]); | ||
| if (inputArgNr < parameters.size()) | ||
| parameterLength = ValueFlow::valueFlowGetStrLength(parameters[inputArgNr], settings); | ||
Check failureCode scanning / Cppcheck Premium Guarantee that container indices and iterators are within the valid range Critical
Guarantee that container indices and iterators are within the valid range
|
||
|
Collaborator
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. Looks like a false positive.
Collaborator
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 agree. And it might be fixed in latest cppcheck premium I fail to reproduce.. |
||
|
|
||
| handleNextParameter = true; | ||
| break; | ||
|
|
@@ -602,7 +603,7 @@ | |
| switch (minsize.type) { | ||
| case Library::ArgumentChecks::MinSize::Type::STRLEN: | ||
| if (settings.library.isargformatstr(ftok, minsize.arg)) { | ||
| return getMinFormatStringOutputLength(args, minsize.arg) < bufferSize; | ||
| return getMinFormatStringOutputLength(args, minsize.arg, settings) < bufferSize; | ||
| } else if (arg) { | ||
| const Token *strtoken = arg->getValueTokenMaxStrLength(); | ||
| if (strtoken) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4610,6 +4610,26 @@ class TestBufferOverrun : public TestFixture { | |
| " mysprintf(a, \"abcd\");\n" | ||
| "}", settings); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void f() {\n" // #901 | ||
|
Collaborator
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. Are there some related test to ensure there is not false positives. I.e. if a size is 2 instead. |
||
| " const char b[] = \"b\";\n" | ||
| " char a[1];\n" | ||
| " sprintf(a, \"%s\", b);\n" | ||
| "}\n" | ||
| "void g() {\n" | ||
| " const char* b = \"b\";\n" | ||
| " char a[1];\n" | ||
| " sprintf(a, \"%s\", b);\n" | ||
| "}\n" | ||
| "void h() {\n" | ||
| " const std::string b = \"b\";\n" | ||
| " char a[1];\n" | ||
| " sprintf(a, \"%s\", b.c_str());\n" | ||
| "}", settings0); | ||
| ASSERT_EQUALS("[test.cpp:4:13]: (error) Buffer is accessed out of bounds: a [bufferAccessOutOfBounds]\n" | ||
| "[test.cpp:9:13]: (error) Buffer is accessed out of bounds: a [bufferAccessOutOfBounds]\n" | ||
| "[test.cpp:14:13]: (error) Buffer is accessed out of bounds: a [bufferAccessOutOfBounds]\n", | ||
| errout_str()); | ||
| } | ||
|
|
||
| void minsize_mul() { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.