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
6 changes: 4 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2824,8 +2824,10 @@ int numberOfArguments(const Token* ftok) {

int numberOfArgumentsWithoutAst(const Token* start)
{
int arguments=0;
const Token* const openBracket = start->next();
int arguments = 0;
const Token* openBracket = start->next();
while (Token::simpleMatch(openBracket, ")"))
openBracket = openBracket->next();
if (openBracket && openBracket->str()=="(" && openBracket->next() && openBracket->next()->str()!=")") {
const Token* argument=openBracket->next();
while (argument) {
Expand Down
4 changes: 3 additions & 1 deletion lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ std::string Library::getFunctionName(const Token *ftok, bool *error) const

std::string Library::getFunctionName(const Token *ftok) const
{
if (!Token::Match(ftok, "%name% (") && (ftok->strAt(-1) != "&" || ftok->previous()->astOperand2()))
if (!Token::Match(ftok, "%name% )| (") && (ftok->strAt(-1) != "&" || ftok->previous()->astOperand2()))
return "";

// Lookup function name using AST..
Expand Down Expand Up @@ -1228,6 +1228,8 @@ bool Library::isNotLibraryFunction(const Token *ftok) const

bool Library::matchArguments(const Token *ftok, const std::string &functionName) const
{
if (functionName.empty())
return false;
const int callargs = numberOfArgumentsWithoutAst(ftok);
const std::unordered_map<std::string, Function>::const_iterator it = functions.find(functionName);
if (it == functions.cend())
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8163,7 +8163,7 @@ void Tokenizer::simplifyDeclspec()
void Tokenizer::simplifyAttribute()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "%type% (") && !mSettings->library.isNotLibraryFunction(tok)) {
if (!tok->isKeyword() && Token::Match(tok, "%type% (") && !mSettings->library.isNotLibraryFunction(tok)) {
if (mSettings->library.isFunctionConst(tok->str(), true))
tok->isAttributePure(true);
if (mSettings->library.isFunctionConst(tok->str(), false))
Expand Down
10 changes: 6 additions & 4 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ void combineValueProperties(const ValueFlow::Value &value1, const ValueFlow::Val
result->path = value1.path;
}

static const Token *getCastTypeStartToken(const Token *parent)
static const Token *getCastTypeStartToken(const Token *parent, const Settings* settings)
{
// TODO: This might be a generic utility function?
if (!Token::Match(parent, "{|("))
Expand All @@ -470,7 +470,8 @@ static const Token *getCastTypeStartToken(const Token *parent)
return parent->astOperand1();
if (parent->str() != "(")
return nullptr;
if (!parent->astOperand2() && Token::Match(parent,"( %name%"))
if (!parent->astOperand2() && Token::Match(parent, "( %name%") &&
(parent->next()->isStandardType() || settings->library.isNotLibraryFunction(parent->next())))
return parent->next();
if (parent->astOperand2() && Token::Match(parent->astOperand1(), "const_cast|dynamic_cast|reinterpret_cast|static_cast <"))
return parent->astOperand1()->tokAt(2);
Expand Down Expand Up @@ -603,7 +604,8 @@ static void setTokenValue(Token* tok,
return !Token::simpleMatch(p, ",");
});
// Ensure that the comma isn't a function call
if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{"))) {
if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{") &&
(!Token::Match(callParent, "( %name%") || settings->library.isNotLibraryFunction(callParent->next())))) {
setTokenValue(parent, std::move(value), settings);
return;
}
Expand Down Expand Up @@ -729,7 +731,7 @@ static void setTokenValue(Token* tok,
}

// cast..
if (const Token *castType = getCastTypeStartToken(parent)) {
if (const Token *castType = getCastTypeStartToken(parent, settings)) {
if (contains({ValueFlow::Value::ValueType::INT, ValueFlow::Value::ValueType::SYMBOLIC}, value.valueType) &&
Token::simpleMatch(parent->astOperand1(), "dynamic_cast"))
return;
Expand Down
11 changes: 9 additions & 2 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2838,14 +2838,21 @@ class TestNullPointer : public TestFixture {
ASSERT_EQUALS("[test.cpp:7]: (warning) Possible null pointer dereference: p\n", errout.str());
}

void nullpointer_cast() { // #4692
check("char *nasm_skip_spaces(const char *p) {\n"
void nullpointer_cast() {
check("char *nasm_skip_spaces(const char *p) {\n" // #4692
" if (p)\n"
" while (*p && nasm_isspace(*p))\n"
" p++;\n"
" return p;\n"
"}");
ASSERT_EQUALS("", errout.str());

check("void f(char* origin) {\n" // #11449
" char* cp = (strchr)(origin, '\\0');\n"
" if (cp[-1] != '/')\n"
" *cp++ = '/';\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void nullpointer_castToVoid() { // #3771
Expand Down