Partial fix for #11626 FN functionConst with non-dereferenceable pointer access (#5070)

This commit is contained in:
chrchr-github 2023-05-21 14:00:24 +02:00 committed by GitHub
parent ba57e15cb2
commit d30f8e18f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -2364,7 +2364,9 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
}
// non const pointer cast
if (tok1->valueType() && tok1->valueType()->pointer > 0 && tok1->astParent() && tok1->astParent()->isCast() && !Token::simpleMatch(tok1->astParent(), "( const"))
if (tok1->valueType() && tok1->valueType()->pointer > 0 && tok1->astParent() && tok1->astParent()->isCast() &&
!(tok1->astParent()->valueType() &&
(tok1->astParent()->valueType()->pointer == 0 || tok1->astParent()->valueType()->isConst(tok1->astParent()->valueType()->pointer))))
return false;
const Token* lhs = tok1->previous();

View File

@ -197,6 +197,7 @@ private:
TEST_CASE(const84);
TEST_CASE(const85);
TEST_CASE(const86);
TEST_CASE(const87);
TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
@ -6398,6 +6399,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const87() { // #11626
checkConst("struct S {\n"
" bool f() { return static_cast<bool>(p); }\n"
" const int* g() { return const_cast<const int*>(p); }\n"
" const int* h() { return (const int*)p; }\n"
" char* j() { return reinterpret_cast<char*>(p); }\n"
" char* k() { return (char*)p; }\n"
" int* p;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'S::f' can be const.\n"
"[test.cpp:3]: (style, inconclusive) Technically the member function 'S::g' can be const.\n"
"[test.cpp:4]: (style, inconclusive) Technically the member function 'S::h' can be const.\n",
errout.str());
}
void const_handleDefaultParameters() {
checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n"