Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,10 @@ void CheckLeakAutoVar::check()
if (scope->hasInlineOrLambdaFunction())
continue;

recursiveCount = 0;

// Empty variable info
VarInfo varInfo;

checkScope(scope->bodyStart, &varInfo, notzero);
checkScope(scope->bodyStart, &varInfo, notzero, 0);

varInfo.conditionalAlloc.clear();

Expand Down Expand Up @@ -236,12 +234,13 @@ static const Token * isFunctionCall(const Token * nameToken)

void CheckLeakAutoVar::checkScope(const Token * const startToken,
VarInfo *varInfo,
std::set<unsigned int> notzero)
std::set<unsigned int> notzero,
unsigned int recursiveCount)
{
// The C++ standard suggests a minimum of 256 nested control statements
// but MSVC has a limit of 100. Cppcheck is hitting 256 when checking itself.
if (++recursiveCount > 384)
throw InternalError(startToken, "Internal limit: CheckLeakAutoVar::checkScope() Maximum recursive count of 384 reached.", InternalError::LIMIT);
// but MSVC has a limit of 100.
if (++recursiveCount > 100)
throw InternalError(startToken, "Internal limit: CheckLeakAutoVar::checkScope() Maximum recursive count of 100 reached.", InternalError::LIMIT);

std::map<unsigned int, VarInfo::AllocInfo> &alloctype = varInfo->alloctype;
std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;
Expand Down Expand Up @@ -471,10 +470,10 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
}
}

checkScope(closingParenthesis->next(), &varInfo1, notzero);
checkScope(closingParenthesis->next(), &varInfo1, notzero, recursiveCount);
closingParenthesis = closingParenthesis->linkAt(1);
if (Token::simpleMatch(closingParenthesis, "} else {")) {
checkScope(closingParenthesis->tokAt(2), &varInfo2, notzero);
checkScope(closingParenthesis->tokAt(2), &varInfo2, notzero, recursiveCount);
tok = closingParenthesis->linkAt(2)->previous();
} else {
tok = closingParenthesis->previous();
Expand Down
9 changes: 4 additions & 5 deletions lib/checkleakautovar.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ class CPPCHECKLIB VarInfo {
class CPPCHECKLIB CheckLeakAutoVar : public Check {
public:
/** This constructor is used when registering the CheckLeakAutoVar */
CheckLeakAutoVar() : Check(myName()), recursiveCount(0) {
CheckLeakAutoVar() : Check(myName()) {
}

/** This constructor is used when running checks. */
CheckLeakAutoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger), recursiveCount(0) {
: Check(myName(), tokenizer, settings, errorLogger) {
}

void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
Expand All @@ -116,7 +116,8 @@ class CPPCHECKLIB CheckLeakAutoVar : public Check {
/** check for leaks in a function scope */
void checkScope(const Token * const startToken,
VarInfo *varInfo,
std::set<unsigned int> notzero);
std::set<unsigned int> notzero,
unsigned int recursiveCount);

/** Check token inside expression.
* @param tok token inside expression.
Expand Down Expand Up @@ -163,8 +164,6 @@ class CPPCHECKLIB CheckLeakAutoVar : public Check {
std::string classInfo() const OVERRIDE {
return "Detect when a auto variable is allocated but not deallocated or deallocated twice.\n";
}

unsigned int recursiveCount;
};
/// @}
//---------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,14 @@ class TestLeakAutoVar : public TestFixture {
" if (0) { }\n"
" THOU\n"
"}"), InternalError);
ASSERT_NO_THROW(checkP("#define ONE if (0) { }\n"
"#define TEN ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE\n"
"#define HUN TEN TEN TEN TEN TEN TEN TEN TEN TEN TEN\n"
"#define THOU HUN HUN HUN HUN HUN HUN HUN HUN HUN HUN\n"
"void foo() {\n"
" if (0) { }\n"
" THOU\n"
"}"));
}

};
Expand Down