diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 158bcac04..2cf931103 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -181,7 +181,10 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok const Library::AllocFunc *f = mSettings_->library.getReallocFuncInfo(tok2); if (!(f && f->reallocArg > 0 && f->reallocArg <= numberOfArguments(tok2))) return No; - const Token* arg = getArguments(tok2).at(f->reallocArg - 1); + const auto args = getArguments(tok2); + if (args.size() < (f->reallocArg)) + return No; + const Token* arg = args.at(f->reallocArg - 1); while (arg && arg->isCast()) arg = arg->astOperand1(); while (arg && arg->isUnaryOp("*")) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 4d0463979..e4ea82944 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2177,6 +2177,8 @@ private: // Test getAllocationType for subfunction TEST_CASE(getAllocationType); + + TEST_CASE(crash1); // #10729 } void functionParameter() { @@ -2565,6 +2567,18 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); } + + void crash1() { // #10729 + check("void foo() {\n" + " extern void *realloc (void *ptr, size_t size);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void foo() {\n" + " extern void *malloc (size_t size);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestMemleakNoVar)