Skip to content

Commit

Permalink
Enable validateVariables(), fix fuzzing crash
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 12, 2024
1 parent 54f46d3 commit 25c562d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
17 changes: 13 additions & 4 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,17 @@ namespace {
}
}
}
if (const Scope* scope = var->nameToken()->scope()) {
auto it = std::find_if(scope->functionList.begin(), scope->functionList.end(), [&](const Function& function) {
for (std::size_t arg = 0; arg < function.argCount(); ++arg) {
if (var == function.getArgumentVar(arg))
return true;
}
return false;
});
if (it != scope->functionList.end())
return &*it;
}
return nullptr;
}
}
Expand All @@ -2172,9 +2183,8 @@ void SymbolDatabase::validateVariables() const
if (var) {
if (!var->scope()) {
const Function* function = getFunctionForArgumentvariable(var, functionScopes);
if (!var->isArgument() || (function && function->hasBody())) {
if (!var->isArgument() || (!function || function->hasBody())) {
throw InternalError(var->nameToken(), "Analysis failed (variable without scope). If the code is valid then please report this failure.", InternalError::INTERNAL);
//std::cout << "!!!Variable found without scope: " << var->nameToken()->str() << std::endl;
}
}
}
Expand All @@ -2186,8 +2196,7 @@ void SymbolDatabase::validate() const
if (mSettings.debugwarnings) {
validateExecutableScopes();
}
// TODO
//validateVariables();
validateVariables();
}

void SymbolDatabase::clangSetVariables(const std::vector<const Variable *> &variableList)
Expand Down
12 changes: 6 additions & 6 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1394,12 +1394,6 @@ class CPPCHECKLIB SymbolDatabase {
*/
void validate() const;

void validateExecutableScopes() const;
/**
* @brief Check variable list, e.g. variables w/o scope
*/
void validateVariables() const;

/** Set valuetype in provided tokenlist */
void setValueTypeInTokenList(bool reportDebugWarnings, Token *tokens=nullptr);

Expand Down Expand Up @@ -1467,6 +1461,12 @@ class CPPCHECKLIB SymbolDatabase {
void setValueType(Token* tok, const Variable& var, const SourceLocation &loc = SourceLocation::current());
void setValueType(Token* tok, const Enumerator& enumerator, const SourceLocation &loc = SourceLocation::current());

void validateExecutableScopes() const;
/**
* @brief Check variable list, e.g. variables w/o scope
*/
void validateVariables() const;

Tokenizer& mTokenizer;
const Settings &mSettings;
ErrorLogger *mErrorLogger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
i a;m t(=a[]);m e(){a[]=0}
33 changes: 17 additions & 16 deletions test/testgarbage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,22 +1625,23 @@ class TestGarbage : public TestFixture {
}

void garbageCode205() {
checkCode("class CodeSnippetsEvent : public wxCommandEvent {\n"
"public :\n"
" CodeSnippetsEvent ( wxEventType commandType = wxEventType , int id = 0 ) ;\n"
" CodeSnippetsEvent ( const CodeSnippetsEvent & event ) ;\n"
"virtual wxEvent * Clone ( ) const { return new CodeSnippetsEvent ( * this ) ; }\n"
"private :\n"
" int m_SnippetID ;\n"
"} ;\n"
"const wxEventType wxEVT_CODESNIPPETS_GETFILELINKS = wxNewEventType ( )\n"
"CodeSnippetsEvent :: CodeSnippetsEvent ( wxEventType commandType , int id )\n"
": wxCommandEvent ( commandType , id ) {\n"
"}\n"
"CodeSnippetsEvent :: CodeSnippetsEvent ( const CodeSnippetsEvent & Event )\n"
": wxCommandEvent ( Event )\n"
", m_SnippetID ( 0 ) {\n"
"}"); // don't crash
ASSERT_THROW(checkCode("class CodeSnippetsEvent : public wxCommandEvent {\n"
"public :\n"
" CodeSnippetsEvent ( wxEventType commandType = wxEventType , int id = 0 ) ;\n"
" CodeSnippetsEvent ( const CodeSnippetsEvent & event ) ;\n"
"virtual wxEvent * Clone ( ) const { return new CodeSnippetsEvent ( * this ) ; }\n"
"private :\n"
" int m_SnippetID ;\n"
"} ;\n"
"const wxEventType wxEVT_CODESNIPPETS_GETFILELINKS = wxNewEventType ( )\n"
"CodeSnippetsEvent :: CodeSnippetsEvent ( wxEventType commandType , int id )\n"
": wxCommandEvent ( commandType , id ) {\n"
"}\n"
"CodeSnippetsEvent :: CodeSnippetsEvent ( const CodeSnippetsEvent & Event )\n"
": wxCommandEvent ( Event )\n"
", m_SnippetID ( 0 ) {\n"
"}"),
InternalError);
}

void garbageCode206() {
Expand Down

0 comments on commit 25c562d

Please sign in to comment.