Fix 10953: False positive: Possible null pointer dereference when calling derived function (#4044)

This commit is contained in:
Paul Fultz II 2022-04-26 10:57:06 -05:00 committed by GitHub
parent 7d723a7a60
commit 474c7fe5cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -7008,6 +7008,9 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
function = tok->astOperand1()->function();
if (!function)
continue;
// TODO: Check if member variable is a pointer or reference
if (function->isImplicitlyVirtual() && !function->hasFinalSpecifier())
continue;
if (tok->hasKnownValue())
continue;

View File

@ -4341,8 +4341,51 @@ private:
" x = 10 - f1(2);\n"
" }\n"
"};";
ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue);
TODO_ASSERT_EQUALS(7, 0, valueOfTok(code, "-").intvalue);
ASSERT_EQUALS(false, valueOfTok(code, "-").isKnown());
code = "struct base {\n"
" virtual int f() { return 0; }\n"
"};\n"
"void g(base* b) {\n"
" int x = b->f();\n"
" return x;\n"
"}\n";
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 6U, 0));
ASSERT_EQUALS(false, testValueOfXKnown(code, 6U, 0));
code = "struct base {\n"
" virtual int f() { return 0; }\n"
"};\n"
"void g(base& b) {\n"
" int x = b.f();\n"
" return x;\n"
"}\n";
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 6U, 0));
ASSERT_EQUALS(false, testValueOfXKnown(code, 6U, 0));
code = "struct base {\n"
" virtual int f() { return 0; }\n"
"};\n"
"struct derived {\n"
" virtual int f() { return 1; }\n"
"};\n"
"void g(derived* d) {\n"
" base* b = d;\n"
" int x = b->f();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 10U, 0));
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 10U, 1));
code = "struct base {\n"
" virtual int f() final { return 0; }\n"
"};\n"
"void g(base* b) {\n"
" int x = b->f();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 6U, 0));
}
void valueFlowFunctionDefaultParameter() {