Fix #11732 FP autoVariables when reassigning argv (#5084)

This commit is contained in:
chrchr-github 2023-05-28 13:51:41 +02:00 committed by GitHub
parent 647432580f
commit 163fb79de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -55,10 +55,10 @@ static bool isPtrArg(const Token *tok)
return (var && var->isArgument() && var->isPointer());
}
static bool isArrayArg(const Token *tok)
static bool isArrayArg(const Token *tok, const Settings* settings)
{
const Variable *var = tok->variable();
return (var && var->isArgument() && var->isArray());
return (var && var->isArgument() && var->isArray() && !settings->library.isentrypoint(var->scope()->className));
}
static bool isArrayVar(const Token *tok)
@ -266,7 +266,7 @@ void CheckAutoVariables::autoVariables()
}
tok = tok->tokAt(4);
} else if (Token::Match(tok, "[;{}] %var% [") && Token::simpleMatch(tok->linkAt(2), "] =") &&
(isPtrArg(tok->next()) || isArrayArg(tok->next())) && isAddressOfLocalVariable(tok->linkAt(2)->next()->astOperand2())) {
(isPtrArg(tok->next()) || isArrayArg(tok->next(), mSettings)) && isAddressOfLocalVariable(tok->linkAt(2)->next()->astOperand2())) {
errorAutoVariableAssignment(tok->next(), false);
}
// Invalid pointer deallocation

View File

@ -68,6 +68,7 @@ private:
TEST_CASE(testautovar16); // ticket #8114
TEST_CASE(testautovar_array1);
TEST_CASE(testautovar_array2);
TEST_CASE(testautovar_array3);
TEST_CASE(testautovar_normal); // "normal" token list that does not remove casts etc
TEST_CASE(testautovar_ptrptr); // ticket #6956
TEST_CASE(testautovar_return1);
@ -517,6 +518,14 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
}
void testautovar_array3() {
check("int main(int argc, char* argv[]) {\n" // #11732
" std::string a = \"abc\";\n"
" argv[0] = &a[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void testautovar_normal() {
check("void f(XmDestinationCallbackStruct *ds)\n"
"{\n"