From 59e4eefc2ed134b705f1ba3f5ce67c1342cec4cd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:25:20 +0200 Subject: [PATCH] Fix #13199 nullptr dereference in checkInnerScope() (#6878) --- lib/checkother.cpp | 10 ++++++---- test/testother.cpp | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3b26385c6cd..8401d918ecc 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1254,10 +1254,12 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us } if (ftok->function()) { const std::list &argvars = ftok->function()->argumentList; - const Variable *argvar = ftok->function()->getArgumentVar(argn); - if (!std::all_of(argvars.cbegin(), argvars.cend(), [&](const Variable &other) { - return &other == argvar || !mayDependOn(other.valueType(), argvar->valueType()); - })) return false; + if (const Variable* argvar = ftok->function()->getArgumentVar(argn)) { + if (!std::all_of(argvars.cbegin(), argvars.cend(), [&](const Variable& other) { + return &other == argvar || !mayDependOn(other.valueType(), argvar->valueType()); + })) + return false; + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index cc4406a8a7e..6fd52f2e06c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1843,6 +1843,14 @@ class TestOther : public TestFixture { " printf(\"result: %d\\n\", msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("void g(const char* format, ...);\n" + "void f(bool b) {\n" + " const char* s = \"abc\";\n" + " if (b)\n" + " g(\"%d %s\", 1, s);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) The scope of the variable 's' can be reduced.\n", errout_str()); } #define checkOldStylePointerCast(...) checkOldStylePointerCast_(__FILE__, __LINE__, __VA_ARGS__)