Repository navigation
Fix #11765 FN: minsize not checked for string literal, buffer access out of bounds not found #8944
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 |
|---|---|---|
|
|
@@ -558,7 +558,7 @@ void CheckBufferOverrunImpl::pointerArithmeticError(const Token *tok, const Toke | |
|
|
||
| //--------------------------------------------------------------------------- | ||
|
|
||
| ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, const Settings& settings) const | ||
| ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, const Settings& settings, bool checkStringLiteral) const | ||
| { | ||
| if (!bufTok->valueType()) | ||
| return ValueFlow::Value(-1); | ||
|
|
@@ -604,6 +604,14 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons | |
| return bufSizeVal; | ||
| } | ||
| } | ||
| } else if (checkStringLiteral && bufTok->tokType() == Token::Type::eString && bufTok->valueType()) { | ||
| ValueFlow::Value strSizeVal; | ||
| strSizeVal.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE; | ||
| const size_t charSize = bufTok->valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee); | ||
| if (charSize > 0) { | ||
| strSizeVal.intvalue = Token::getStrArraySize(bufTok) * charSize; | ||
| return strSizeVal; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -715,7 +723,8 @@ void CheckBufferOverrunImpl::bufferOverflow() | |
| if (argtok->valueType() && argtok->valueType()->pointer == 0) | ||
| continue; | ||
| // TODO: strcpy(buf+10, "hello"); | ||
| ValueFlow::Value bufferSize = getBufferSize(argtok, mSettings); | ||
| const bool strz = mSettings.library.isargstrz(tok, argnr + 1); | ||
|
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. AI review This check only helps if the cfg marks the argument
So |
||
| ValueFlow::Value bufferSize = getBufferSize(argtok, mSettings, !strz); | ||
| if (bufferSize.intvalue <= 0) | ||
| continue; | ||
| // buffer size == 1 => do not warn for dynamic memory | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -719,6 +719,13 @@ size_t bufferAccessOutOfBounds_strnlen(const char *s, size_t maxlen) | |
| size_t len = strnlen(buf,2); | ||
| // cppcheck-suppress bufferAccessOutOfBounds | ||
| len+=strnlen(buf,3); | ||
| len+=strnlen("ab",3); | ||
|
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. AI review This line doesn't test the change. |
||
| return len; | ||
| } | ||
|
|
||
| size_t bufferAccessOutOfBounds_strncasecmp(const char *s) | ||
| { | ||
| size_t len = strncasecmp(s, "abc", 5); | ||
| return len; | ||
| } | ||
|
|
||
|
|
@@ -1606,4 +1613,4 @@ void pthread_define_test() | |
| (void)rwlock; | ||
| PTHREAD_SCOPE_PROCESS; | ||
| PTHREAD_SCOPE_SYSTEM; | ||
| } | ||
| } | ||
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.
AI review
<strz/>also turns on theinvalidFunctionArgStrcheck in CheckFunctions, which requires a NUL-terminated argument.strncasecmpreads at mostnbytes, so a non-terminated buffer is valid here. With this change,char buf[3] = {'a','b','c'}; strncasecmp(buf, s, 3);andchar c = 'a'; strncasecmp(&c, s, 1);giveerror: Invalid strncasecmp() argument nr 1. A nul-terminated string is required. [invalidFunctionArgStr], and base reports nothing. The same applies to arg 2 (line 2132). Adding<strz/>the same way to the other bounded functions (strnlen,_strnicmp, ...) would spread this false positive.