Skip to content

Commit

Permalink
Fix #12335 FP checkLibraryFunction for incorrect scope (#5868)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 12, 2024
1 parent 2f606e9 commit 3fb85f9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,12 @@ std::string Library::getFunctionName(const Token *ftok) const
if (ftok->astParent()) {
bool error = false;
const Token * tok = ftok->astParent()->isUnaryOp("&") ? ftok->astParent()->astOperand1() : ftok->next()->astOperand1();
const std::string ret = getFunctionName(tok, error);
return error ? std::string() : ret;
std::string ret = getFunctionName(tok, error);
if (error)
return {};
if (startsWith(ret, "::"))
ret.erase(0, 2);
return ret;
}

// Lookup function name without using AST..
Expand Down
16 changes: 14 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9647,6 +9647,18 @@ void Tokenizer::simplifyBitfields()
}
}

static bool isStdContainerOrIterator(const Token* tok, const Settings* settings)
{
const Library::Container* ctr = settings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true);
return ctr && startsWith(ctr->startPattern, "std ::");
}

static bool isStdSmartPointer(const Token* tok, const Settings* settings)
{
const Library::SmartPointer* ptr = settings->library.detectSmartPointer(tok, /*withoutStd*/ true);
return ptr && startsWith(ptr->name, "std::");
}

// Add std:: in front of std classes, when using namespace std; was given
void Tokenizer::simplifyNamespaceStd()
{
Expand Down Expand Up @@ -9677,11 +9689,11 @@ void Tokenizer::simplifyNamespaceStd()
if (userFunctions.find(tok->str()) == userFunctions.end() && mSettings->library.matchArguments(tok, "std::" + tok->str()))
insert = true;
} else if (Token::simpleMatch(tok->next(), "<") &&
(mSettings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true) || mSettings->library.detectSmartPointer(tok, /*withoutStd*/ true)))
(isStdContainerOrIterator(tok, mSettings) || isStdSmartPointer(tok, mSettings)))
insert = true;
else if (mSettings->library.hasAnyTypeCheck("std::" + tok->str()) ||
mSettings->library.podtype("std::" + tok->str()) ||
mSettings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true))
isStdContainerOrIterator(tok, mSettings))
insert = true;

if (insert) {
Expand Down
10 changes: 10 additions & 0 deletions test/cfg/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ bool QString7(QString s, const QString& l) {
return l.startsWith(s);
}

namespace NTestStd // #12355
{
using namespace std;
QString QString_std(QString s)
{
s.replace("abc", "def");
return s;
}
}

void QByteArray1(QByteArray byteArrayArg)
{
for (int i = 0; i <= byteArrayArg.size(); ++i) {
Expand Down
6 changes: 6 additions & 0 deletions test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4892,3 +4892,9 @@ void std_vector_data_arithmetic()
buf.resize(1);
memcpy(buf.data() + 0, "", 1);
}

std::string global_scope_std() // #12355
{
::std::stringstream ss;
return ss.str();
}

0 comments on commit 3fb85f9

Please sign in to comment.