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
10 changes: 9 additions & 1 deletion lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ void CheckAutoVariables::errorUselessAssignmentPtrArg(const Token *tok)
"Assignment of function parameter has no effect outside the function. Did you forget dereferencing it?", CWE398, Certainty::normal);
}

bool CheckAutoVariables::diag(const Token* tokvalue)
{
if (!tokvalue)
return true;
return !mDiagDanglingTemp.insert(tokvalue).second;
}

//---------------------------------------------------------------------------

static bool isInScope(const Token * tok, const Scope * scope)
Expand Down Expand Up @@ -603,7 +610,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
break;
} else if (!tokvalue->variable() &&
isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {
errorDanglingTemporaryLifetime(tok, &val, tokvalue);
if (!diag(tokvalue))
errorDanglingTemporaryLifetime(tok, &val, tokvalue);
break;
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/checkautovariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "errortypes.h"

#include <string>
#include <set>

class Settings;
class Token;
Expand Down Expand Up @@ -127,6 +128,11 @@ class CPPCHECKLIB CheckAutoVariables : public Check {
"- suspicious assignment of pointer argument\n"
"- useless assignment of function argument\n";
}

/** returns true if tokvalue has already been diagnosed */
bool diag(const Token* tokvalue);

std::set<const Token*> mDiagDanglingTemp;
};
/// @}
//---------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,12 @@ class TestAutoVariables : public TestFixture {
" return sv;\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f() {\n" // #10993
" std::string_view v = std::string();\n"
" v.data();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2] -> [test.cpp:3]: (error) Using object that is a temporary.\n", errout.str());
}

void danglingLifetimeUniquePtr()
Expand Down