Skip to content

Commit bfaa7c0

Browse files
chrchr-githubchrchr-github
andauthored
Fix #11845 FP variableScope if buffer is passed to a conditionally called function (#5265)
Co-authored-by: chrchr-github <chrchr@github>
1 parent 99f7f88 commit bfaa7c0

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

lib/checkother.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ void CheckOther::checkVariableScope()
991991
}
992992
}
993993

994-
bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& used)
994+
bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& used) const
995995
{
996996
const Scope* scope = tok->next()->scope();
997997
bool loopVariable = scope->isLoopScope();
@@ -1071,6 +1071,16 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
10711071
if (scope->bodyStart && scope->bodyStart->isSimplifiedScope())
10721072
return false; // simplified if/for/switch init statement
10731073
}
1074+
if (var->isArrayOrPointer()) {
1075+
int argn{};
1076+
if (const Token* ftok = getTokenArgumentFunction(tok, argn)) { // var passed to function?
1077+
if (ftok->function() && Function::returnsPointer(ftok->function()))
1078+
return false;
1079+
const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned
1080+
if (!ret.empty() && ret.back() == '*')
1081+
return false;
1082+
}
1083+
}
10741084
}
10751085
}
10761086

lib/checkother.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class CPPCHECKLIB CheckOther : public Check {
123123

124124
/** @brief %Check scope of variables */
125125
void checkVariableScope();
126-
static bool checkInnerScope(const Token *tok, const Variable* var, bool& used);
126+
bool checkInnerScope(const Token *tok, const Variable* var, bool& used) const;
127127

128128
/** @brief %Check for comma separated statements in return */
129129
void checkCommaSeparatedReturn();

test/testother.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class TestOther : public TestFixture {
106106
TEST_CASE(varScope32); // #11441
107107
TEST_CASE(varScope33);
108108
TEST_CASE(varScope34);
109+
TEST_CASE(varScope35);
109110

110111
TEST_CASE(oldStylePointerCast);
111112
TEST_CASE(invalidPointerCast);
@@ -1636,6 +1637,27 @@ class TestOther : public TestFixture {
16361637
ASSERT_EQUALS("", errout.str());
16371638
}
16381639

1640+
void varScope35() { // #11845
1641+
check("void f(int err, const char* src) {\n"
1642+
" const char* msg = \"Success\";\n"
1643+
" char buf[42];\n"
1644+
" if (err != 0)\n"
1645+
" msg = strcpy(buf, src);\n"
1646+
" printf(\"%d: %s\\n\", err, msg);\n"
1647+
"}\n");
1648+
ASSERT_EQUALS("", errout.str());
1649+
1650+
check("char* g(char* dst, const char* src);\n"
1651+
"void f(int err, const char* src) {\n"
1652+
" const char* msg = \"Success\";\n"
1653+
" char buf[42];\n"
1654+
" if (err != 0)\n"
1655+
" msg = g(buf, src);\n"
1656+
" printf(\"%d: %s\\n\", err, msg);\n"
1657+
"}\n");
1658+
ASSERT_EQUALS("", errout.str());
1659+
}
1660+
16391661
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
16401662
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
16411663
// Clear the error buffer..

0 commit comments

Comments
 (0)