-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #11691: FP throwInNoexceptFunction with if constexpr in template #6962
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
#include "symboldatabase.h" | ||
#include "token.h" | ||
|
||
#include <algorithm> | ||
#include <list> | ||
#include <set> | ||
#include <utility> | ||
|
@@ -239,6 +240,13 @@ void CheckExceptionSafety::catchExceptionByValueError(const Token *tok) | |
"as a (const) reference which is usually recommended in C++.", CWE398, Certainty::normal); | ||
} | ||
|
||
static bool isPossibleBool(const Token *tok, bool b) | ||
{ | ||
const std::list<ValueFlow::Value> &values = tok->values(); | ||
return std::any_of(values.cbegin(), values.cend(), [&](const ValueFlow::Value &value) { | ||
return value.isIntValue() && value.isPossible() && value.intvalue == static_cast<int>(b); | ||
}); | ||
} | ||
|
||
static const Token * functionThrowsRecursive(const Function * function, std::set<const Function *> & recursive) | ||
{ | ||
|
@@ -251,6 +259,19 @@ static const Token * functionThrowsRecursive(const Function * function, std::set | |
|
||
for (const Token *tok = function->functionScope->bodyStart->next(); | ||
tok != function->functionScope->bodyEnd; tok = tok->next()) { | ||
if (Token::simpleMatch(tok, "if")) { | ||
tok = tok->astParent(); | ||
const Token *condTok = tok->astOperand2(); | ||
// check if condition is possibly false, since template arguments are not set to known | ||
if (isPossibleBool(condTok, false)) { | ||
tok = tok->link()->next(); | ||
if (Token::simpleMatch(tok, "{")) | ||
tok = tok->link(); | ||
else while (tok && !Token::simpleMatch(tok, ";")) | ||
tok = tok->next(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this does not seem to be formatted properly. we have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this seems to be what uncrustify wants. But I could add braces to make it more readable. |
||
continue; | ||
} | ||
} | ||
if (Token::simpleMatch(tok, "try {")) | ||
tok = tok->linkAt(1); // skip till start of catch clauses | ||
if (tok->str() == "throw") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -361,6 +361,27 @@ class TestExceptionSafety : public TestFixture { | |
// avoid false positives | ||
check("const char *func() throw() { return 0; }"); | ||
ASSERT_EQUALS("", errout_str()); | ||
|
||
|
||
// #11691: FP throwInNoexceptFunction with if constexpr in template | ||
check("template<bool IsNoThrow>\n" | ||
"static void* MyNew(size_t Size) noexcept(IsNoThrow) {\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test case can be reduced:
|
||
" void* Memory = std::malloc(Size);\n" | ||
" if (!Memory) {\n" | ||
" if constexpr (!IsNoThrow) {\n" | ||
" throw std::bad_alloc();\n" | ||
" }\n" | ||
" }\n" | ||
" return Memory;\n" | ||
"}\n" | ||
"void* AllocateMemory(size_t Size, bool UseNoThrow) {\n" | ||
" if (UseNoThrow) {\n" | ||
" return MyNew<true>(Size);\n" | ||
" } else {\n" | ||
" return MyNew<false>(Size);\n" | ||
" }\n" | ||
"}\n"); | ||
ASSERT_EQUALS("", errout_str()); | ||
} | ||
|
||
void unhandledExceptionSpecification1() { // #4800 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow we should check if the possible values are template arguments. You can check
Token::templateArg
for "possible" operands .If the condition is false you skip the if-body but if the condition is true you do nothing, you should skip the
else
body then.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The utility function I introduce in #7045 would be useful for this, so maybe I should wait for that one.