Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions lib/checkexceptionsafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "symboldatabase.h"
#include "token.h"

#include <algorithm>
#include <list>
#include <set>
#include <utility>
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)) {
Copy link
Owner

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.

Copy link
Contributor Author

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.

tok = tok->link()->next();
if (Token::simpleMatch(tok, "{"))
tok = tok->link();
else while (tok && !Token::simpleMatch(tok, ";"))
tok = tok->next();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not seem to be formatted properly. we have the runformat script in the cppcheck folder could you run that..

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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")
Expand Down
21 changes: 21 additions & 0 deletions test/testexceptionsafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test case can be reduced:

template<bool IsNoThrow>
static void foo(size_t Size) noexcept(IsNoThrow) {
    if constexpr (!IsNoThrow) {
        throw std::bad_alloc();
    }
}

foo<true>(123);

" 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
Expand Down
Loading