Skip to content

Commit

Permalink
Fix #12335 FP checkLibraryFunction for incorrect scope
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 10, 2024
1 parent 5ef846a commit e12fcb1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions cfg/std.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6645,6 +6645,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<function name="std::deque::push_back,std::deque::push_front,std::list::push_back,std::list::push_front,std::forward_list::push_front,std::queue::push,std::stack::push,std::vector::push_back">
<noreturn>false</noreturn>
<returnValue type="void"/>
<leak-ignore/>
<arg nr="1">
<not-uninit/>
</arg>
Expand Down
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
3 changes: 2 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9664,6 +9664,7 @@ void Tokenizer::simplifyNamespaceStd()
continue;
if (Token::Match(tok->previous(), ".|::|namespace"))
continue;
const Library::Container* ctr{};
if (Token::simpleMatch(tok->next(), "(")) {
if (isFunctionHead(tok->next(), "{"))
userFunctions.insert(tok->str());
Expand All @@ -9681,7 +9682,7 @@ void Tokenizer::simplifyNamespaceStd()
insert = true;
else if (mSettings->library.hasAnyTypeCheck("std::" + tok->str()) ||
mSettings->library.podtype("std::" + tok->str()) ||
mSettings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true))
((ctr = mSettings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true)) != nullptr && startsWith(ctr->startPattern, "std ::")))
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 // #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 @@ -4860,3 +4860,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 e12fcb1

Please sign in to comment.