2009-12-14 20:30:31 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "executionpath.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
// default : bail out if the condition is has variable handling
|
2009-12-25 20:50:23 +01:00
|
|
|
bool ExecutionPath::parseCondition(const Token &tok, std::list<ExecutionPath *> & checks)
|
2009-12-25 19:45:21 +01:00
|
|
|
{
|
2009-12-25 20:50:23 +01:00
|
|
|
if (Token::Match(tok.tokAt(-3), "!!else if ("))
|
|
|
|
{
|
|
|
|
++ifinfo;
|
|
|
|
}
|
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
unsigned int parlevel = 0;
|
|
|
|
for (const Token *tok2 = &tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel == 0)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok2, ";{}"))
|
|
|
|
break;
|
|
|
|
if (tok2->varId() != 0)
|
|
|
|
{
|
2009-12-25 20:50:23 +01:00
|
|
|
if (ifinfo > 1)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
bailOutVar(checks, tok2->varId());
|
2009-12-25 19:45:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-25 20:12:06 +01:00
|
|
|
static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPath *> &checks)
|
2009-12-14 20:30:31 +01:00
|
|
|
{
|
2010-01-08 21:54:24 +01:00
|
|
|
if (!tok || tok->str() == "}" || checks.empty())
|
|
|
|
return 0;
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
const std::auto_ptr<ExecutionPath> check(checks.front()->copy());
|
|
|
|
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "}")
|
|
|
|
return 0;
|
|
|
|
|
2010-01-03 13:30:20 +01:00
|
|
|
if (Token::simpleMatch(tok, "while ("))
|
|
|
|
{
|
|
|
|
// parse condition
|
|
|
|
if (checks.size() > 10 || check->parseCondition(*tok->tokAt(2), checks))
|
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip "while (fgets()!=NULL)"
|
|
|
|
if (Token::simpleMatch(tok, "while ( fgets ("))
|
|
|
|
{
|
|
|
|
const Token *tok2 = tok->tokAt(3)->link();
|
|
|
|
if (Token::simpleMatch(tok2, ") ) {"))
|
|
|
|
{
|
|
|
|
tok = tok2->tokAt(2)->link();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-10 22:05:51 +01:00
|
|
|
// for/while/switch/do .. bail out
|
2010-01-02 09:08:36 +01:00
|
|
|
if (Token::Match(tok, "for|while|switch|do"))
|
2009-12-14 20:30:31 +01:00
|
|
|
{
|
2010-01-10 22:05:51 +01:00
|
|
|
// goto {
|
|
|
|
const Token *tok2 = tok->next();
|
|
|
|
if (tok2 && tok2->str() == "(")
|
|
|
|
tok2 = tok2->link();
|
|
|
|
if (tok2 && tok2->str() == ")")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (!tok2 || tok2->str() != "{")
|
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip { .. }
|
|
|
|
tok2 = tok2->link();
|
|
|
|
|
|
|
|
// if "do { .. } while ( .." , goto end of while..
|
|
|
|
if (Token::simpleMatch(tok, "do {") && Token::simpleMatch(tok2, "} while ("))
|
|
|
|
tok2 = tok2->tokAt(2)->link();
|
|
|
|
|
2010-02-17 18:10:50 +01:00
|
|
|
// bail out all variables if the scope contains a "return"
|
2010-01-10 22:05:51 +01:00
|
|
|
// bail out all variables used in this for/while/switch/do
|
|
|
|
for (; tok && tok != tok2; tok = tok->next())
|
|
|
|
{
|
2010-02-17 18:10:50 +01:00
|
|
|
if (tok->str() == "return")
|
|
|
|
ExecutionPath::bailOut(checks);
|
2010-01-10 22:05:51 +01:00
|
|
|
if (tok->varId())
|
|
|
|
ExecutionPath::bailOutVar(checks, tok->varId());
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-08 21:24:48 +01:00
|
|
|
// .. ) { ... } => bail out
|
|
|
|
if (Token::simpleMatch(tok, ") {"))
|
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-03 15:35:32 +01:00
|
|
|
if (Token::Match(tok, "abort|exit ("))
|
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-21 18:17:35 +01:00
|
|
|
// don't parse into "struct type { .."
|
2010-02-20 09:35:57 +01:00
|
|
|
if (Token::Match(tok, "struct|union|class %type% {|:"))
|
2010-02-08 18:06:28 +01:00
|
|
|
{
|
|
|
|
while (tok && tok->str() != "{" && tok->str() != ";")
|
|
|
|
tok = tok->next();
|
|
|
|
tok = tok ? tok->link() : 0;
|
|
|
|
}
|
2009-12-21 18:17:35 +01:00
|
|
|
|
2010-01-13 19:37:55 +01:00
|
|
|
if (Token::Match(tok, "= {"))
|
2009-12-14 20:30:31 +01:00
|
|
|
{
|
2010-03-28 10:42:37 +02:00
|
|
|
// GCC struct initialization.. bail out
|
|
|
|
if (Token::Match(tok->tokAt(2), ". %var% ="))
|
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
tok = tok->next()->link();
|
|
|
|
if (!tok)
|
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-01-08 21:24:48 +01:00
|
|
|
// ; { ... }
|
|
|
|
if (Token::Match(tok->previous(), "[;{}] {"))
|
|
|
|
{
|
|
|
|
const Token *tokerr = checkExecutionPaths_(tok->next(), checks);
|
|
|
|
if (tokerr)
|
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
return tokerr;
|
|
|
|
}
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
if (tok->str() == "if")
|
|
|
|
{
|
|
|
|
std::list<ExecutionPath *> newchecks;
|
|
|
|
while (tok->str() == "if")
|
|
|
|
{
|
|
|
|
// goto "("
|
|
|
|
tok = tok->next();
|
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
// parse condition
|
2009-12-26 08:37:07 +01:00
|
|
|
if (checks.size() > 10 || check->parseCondition(*tok->next(), checks))
|
2009-12-14 20:30:31 +01:00
|
|
|
{
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
ExecutionPath::bailOut(newchecks);
|
2009-12-25 19:45:21 +01:00
|
|
|
return 0;
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
2009-12-25 19:45:21 +01:00
|
|
|
// goto ")"
|
|
|
|
tok = tok ? tok->link() : 0;
|
|
|
|
|
2009-12-14 20:30:31 +01:00
|
|
|
// goto "{"
|
|
|
|
tok = tok ? tok->next() : 0;
|
|
|
|
|
|
|
|
if (!Token::simpleMatch(tok, "{"))
|
2009-12-28 19:48:30 +01:00
|
|
|
{
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
ExecutionPath::bailOut(newchecks);
|
2009-12-14 20:30:31 +01:00
|
|
|
return 0;
|
2009-12-28 19:48:30 +01:00
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
// Recursively check into the if ..
|
|
|
|
{
|
|
|
|
std::list<ExecutionPath *> c;
|
|
|
|
std::list<ExecutionPath *>::iterator it;
|
|
|
|
for (it = checks.begin(); it != checks.end(); ++it)
|
|
|
|
c.push_back((*it)->copy());
|
2009-12-25 20:12:06 +01:00
|
|
|
const Token *tokerr = checkExecutionPaths_(tok->next(), c);
|
2009-12-14 20:30:31 +01:00
|
|
|
if (tokerr)
|
2009-12-28 19:48:30 +01:00
|
|
|
{
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(c);
|
|
|
|
ExecutionPath::bailOut(newchecks);
|
2009-12-14 20:30:31 +01:00
|
|
|
return tokerr;
|
2009-12-28 19:48:30 +01:00
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
while (!c.empty())
|
|
|
|
{
|
|
|
|
newchecks.push_back(c.back());
|
|
|
|
c.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// goto "}"
|
|
|
|
tok = tok->link();
|
|
|
|
|
|
|
|
// there is no else => break out
|
|
|
|
if (Token::Match(tok, "} !!else"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// parse next "if"..
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
if (tok->str() == "if")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// there is no "if"..
|
2009-12-25 20:12:06 +01:00
|
|
|
const Token *tokerr = checkExecutionPaths_(tok->next(), checks);
|
2009-12-14 20:30:31 +01:00
|
|
|
if (tokerr)
|
2009-12-28 19:48:30 +01:00
|
|
|
{
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(newchecks);
|
2009-12-14 20:30:31 +01:00
|
|
|
return tokerr;
|
2009-12-28 19:48:30 +01:00
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
|
|
|
|
tok = tok->link();
|
|
|
|
if (!tok)
|
2009-12-28 19:48:30 +01:00
|
|
|
{
|
2009-12-29 09:17:07 +01:00
|
|
|
ExecutionPath::bailOut(newchecks);
|
2009-12-14 20:30:31 +01:00
|
|
|
return 0;
|
2009-12-28 19:48:30 +01:00
|
|
|
}
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::list<ExecutionPath *>::iterator it;
|
|
|
|
for (it = newchecks.begin(); it != newchecks.end(); ++it)
|
2009-12-28 19:48:30 +01:00
|
|
|
checks.push_back(*it);
|
2009-12-14 20:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
bool foundError = false;
|
|
|
|
tok = check->parse(*tok, foundError, checks);
|
|
|
|
if (checks.empty())
|
|
|
|
return 0;
|
|
|
|
else if (foundError)
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2010-01-01 20:12:39 +01:00
|
|
|
// return/throw ends all execution paths
|
|
|
|
if (tok->str() == "return" || tok->str() == "throw")
|
2009-12-14 20:30:31 +01:00
|
|
|
{
|
|
|
|
ExecutionPath::bailOut(checks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-25 20:12:06 +01:00
|
|
|
void checkExecutionPaths(const Token *tok, ExecutionPath *c)
|
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != ")")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Start of implementation..
|
|
|
|
if (Token::Match(tok, ") const| {"))
|
|
|
|
{
|
|
|
|
// goto the "{"
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok->str() == "const")
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
std::list<ExecutionPath *> checks;
|
|
|
|
checks.push_back(c->copy());
|
|
|
|
checkExecutionPaths_(tok, checks);
|
|
|
|
|
|
|
|
c->end(checks, tok->link());
|
|
|
|
|
|
|
|
while (!checks.empty())
|
|
|
|
{
|
|
|
|
delete checks.back();
|
|
|
|
checks.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip this scope - it has been checked
|
|
|
|
tok = tok->link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|