Skip to content

Commit 8205b4a

Browse files
Fix #10572 FP nullPointerRedundantCheck with try/catch / #10701 FP knownConditionTrueFalse with nested try/catch (#5761)
1 parent bc50237 commit 8205b4a

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

lib/forwardanalyzer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,11 @@ namespace {
748748
ForwardTraversal tryTraversal = fork();
749749
tryTraversal.updateRange(tok->next(), endBlock, depth - 1);
750750
bool bail = tryTraversal.actions.isModified();
751-
if (bail)
751+
if (bail) {
752+
actions = tryTraversal.actions;
753+
terminate = tryTraversal.terminate;
752754
return Break();
755+
}
753756

754757
while (Token::simpleMatch(endBlock, "} catch (")) {
755758
Token* endCatch = endBlock->linkAt(2);

test/testcondition.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5232,6 +5232,19 @@ class TestCondition : public TestFixture {
52325232
" }\n"
52335233
"}\n");
52345234
ASSERT_EQUALS("", errout.str());
5235+
5236+
check("void f() {\n" // #10701
5237+
" std::string s;\n"
5238+
" try {\n"
5239+
" try {\n"
5240+
" s = g();\n"
5241+
" }\n"
5242+
" catch (const Err& err) {}\n"
5243+
" }\n"
5244+
" catch (const std::exception& e) {}\n"
5245+
" if (s != \"abc\") {}\n"
5246+
"}\n");
5247+
ASSERT_EQUALS("", errout.str());
52355248
}
52365249

52375250
void multiConditionAlwaysTrue() {

test/testnullpointer.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class TestNullPointer : public TestFixture {
141141
TEST_CASE(nullpointer100); // #11636
142142
TEST_CASE(nullpointer101); // #11382
143143
TEST_CASE(nullpointer102);
144+
TEST_CASE(nullpointer103);
144145
TEST_CASE(nullpointer_addressOf); // address of
145146
TEST_CASE(nullpointerSwitch); // #2626
146147
TEST_CASE(nullpointer_cast); // #4692
@@ -2658,6 +2659,26 @@ class TestNullPointer : public TestFixture {
26582659
ASSERT_EQUALS(
26592660
"[test.cpp:9] -> [test.cpp:8]: (warning) Either the condition 'ptr->y!=nullptr' is redundant or there is possible null pointer dereference: ptr->y.\n",
26602661
errout.str());
2662+
2663+
check("bool argsMatch(const Token *first, const Token *second) {\n" // #6145
2664+
" if (first->str() == \")\")\n"
2665+
" return true;\n"
2666+
" else if (first->next()->str() == \"=\")\n"
2667+
" first = first->nextArgument();\n"
2668+
" else if (second->next()->str() == \"=\") {\n"
2669+
" second = second->nextArgument();\n"
2670+
" if (second)\n"
2671+
" second = second->tokAt(-2);\n"
2672+
" if (!first || !second) {\n"
2673+
" return !first && !second;\n"
2674+
" }\n"
2675+
" }\n"
2676+
" return false;\n"
2677+
"}\n");
2678+
ASSERT_EQUALS(
2679+
"[test.cpp:10] -> [test.cpp:2]: (warning) Either the condition '!first' is redundant or there is possible null pointer dereference: first.\n"
2680+
"[test.cpp:10] -> [test.cpp:4]: (warning) Either the condition '!first' is redundant or there is possible null pointer dereference: first.\n",
2681+
errout.str());
26612682
}
26622683

26632684
void nullpointer90() // #6098
@@ -2858,6 +2879,36 @@ class TestNullPointer : public TestFixture {
28582879
ASSERT_EQUALS("", errout.str());
28592880
}
28602881

2882+
void nullpointer103()
2883+
{
2884+
check("struct S {\n" // #10572
2885+
" int f();\n"
2886+
" int* m_P{};\n"
2887+
"};\n"
2888+
"int S::f() {\n"
2889+
" if (!m_P) {\n"
2890+
" try {\n"
2891+
" m_P = new int(1);\n"
2892+
" }\n"
2893+
" catch (...) {\n"
2894+
" return 0;\n"
2895+
" }\n"
2896+
" }\n"
2897+
" return *m_P;\n"
2898+
"}\n");
2899+
ASSERT_EQUALS("", errout.str());
2900+
2901+
check("void f(int* p, const int* q) {\n" // #11873
2902+
" if (*q == -1)\n"
2903+
" *p = 0;\n"
2904+
"}\n"
2905+
"void g() {\n"
2906+
" int x = -2;\n"
2907+
" f(nullptr, &x);\n"
2908+
"}\n");
2909+
TODO_ASSERT_EQUALS("", "[test.cpp:3]: (warning) Possible null pointer dereference: p\n", errout.str());
2910+
}
2911+
28612912
void nullpointer_addressOf() { // address of
28622913
check("void f() {\n"
28632914
" struct X *x = 0;\n"

test/testunusedvar.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5407,6 +5407,23 @@ class TestUnusedVar : public TestFixture {
54075407
" return 0;\n"
54085408
"}");
54095409
ASSERT_EQUALS("", errout.str());
5410+
5411+
functionVariableUsage("bool argsMatch(const Token *first, const Token *second) {\n" // #6145
5412+
" if (first->str() == \")\")\n"
5413+
" return true;\n"
5414+
" else if (first->next()->str() == \"=\")\n"
5415+
" first = first->nextArgument();\n"
5416+
" else if (second->next()->str() == \"=\") {\n"
5417+
" second = second->nextArgument();\n"
5418+
" if (second)\n"
5419+
" second = second->tokAt(-2);\n"
5420+
" if (!first || !second) {\n"
5421+
" return !first && !second;\n"
5422+
" }\n"
5423+
" }\n"
5424+
" return false;\n"
5425+
"}\n");
5426+
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'first' is assigned a value that is never used.\n", errout.str());
54105427
}
54115428

54125429
void localvarIfElse() {

0 commit comments

Comments
 (0)