diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a5339434a..5d395ccd7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1412,9 +1412,15 @@ void CheckOther::checkConstVariable() bool castToNonConst = false; for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) { if (tok->isCast()) { + if (!tok->valueType()) { + castToNonConst = true; // safe guess + break; + } bool isConst = 0 != (tok->valueType()->constness & (1 << tok->valueType()->pointer)); - if (!isConst) + if (!isConst) { castToNonConst = true; + break; + } } } if (castToNonConst) diff --git a/test/testother.cpp b/test/testother.cpp index 5a2f6bf14..22b5f7c76 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2232,6 +2232,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("struct C { void f() const; };\n" // #9875 - crash + "\n" + "void foo(C& x) {\n" + " x.f();\n" + " foo( static_cast(0) );\n" + "}"); + ASSERT_EQUALS("", errout.str()); check("class a {\n" " void foo(const int& i) const;\n"