From d64dadcd31a16ae23fe468b0d351da4c8e08b12f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 24 Jan 2022 21:50:01 +0100 Subject: [PATCH] Fix #10758 Crash in CheckClass::checkConstFunc() (#3740) --- lib/checkclass.cpp | 2 +- test/testclass.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 300355326..03c2a435d 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2169,7 +2169,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& if (var && var->isStlType(stl_containers_not_const)) return false; const Token* assignTok = end->next()->astParent(); - if (assignTok && assignTok->isAssignmentOp() && assignTok->astOperand1() && assignTok->astOperand1()->variable()) { + if (var && assignTok && assignTok->isAssignmentOp() && assignTok->astOperand1() && assignTok->astOperand1()->variable()) { const Variable* assignVar = assignTok->astOperand1()->variable(); if (assignVar->isPointer() && !assignVar->isConst() && var->typeScope()) { const auto& funcMap = var->typeScope()->functionMap; diff --git a/test/testclass.cpp b/test/testclass.cpp index addc10061..75ed6238e 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -5900,6 +5900,15 @@ private: " }\n" "};\n"); ASSERT_EQUALS("", errout.str()); + + checkConst("struct S {\n" // #10758 + " T* h;\n" + " void f(); \n" + "};\n" + "void S::f() {\n" + " char* c = h->x[y];\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style, inconclusive) Technically the member function 'S::f' can be const.\n", errout.str()); } void const_handleDefaultParameters() {