diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index cd9072622..377adf264 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -968,6 +968,9 @@ public: } const Variable *getVariableFromVarId(std::size_t varId) const { + if (varId >= _variableList.size()) + return nullptr; + return _variableList[varId]; } diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 70eff45ea..3771b863a 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -128,6 +128,7 @@ private: TEST_CASE(arrayMemberVar2); TEST_CASE(arrayMemberVar3); TEST_CASE(staticMemberVar); + TEST_CASE(getVariableFromVarIdBoundsCheck); TEST_CASE(hasRegularFunction); TEST_CASE(hasInlineClassFunction); @@ -750,6 +751,18 @@ private: ASSERT(v && v->isStatic() && v->isConst() && v->isPrivate()); } + void getVariableFromVarIdBoundsCheck() { + GET_SYMBOL_DB("int x;\n" + "int y;\n"); + + const Variable* v = db->getVariableFromVarId(2); + // three elements: varId 0 also counts via a fake-entry + ASSERT(v && db->getVariableListSize() == 3); + + const Variable* v_must_be_null = db->getVariableFromVarId(3); + ASSERT(v_must_be_null == nullptr); + } + void hasRegularFunction() { GET_SYMBOL_DB("void func() { }\n")