diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 98c0e700d83..e6fdbc3bb0d 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -1639,7 +1639,9 @@ void clangimport::parseClangAstDump(Tokenizer &tokenizer, std::istream &f) if (pos1 == std::string::npos) continue; if (!tree.empty() && line.substr(pos1) == "-<<>>") { - const int level = (pos1 - 1) / 2; + const size_t level = (pos1 - 1) / 2; + if (level == 0 || level > tree.size()) + continue; tree[level - 1]->children.push_back(nullptr); continue; } diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index 6a39b563982..26c73e5f7a2 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -139,6 +139,14 @@ class TestClangImport : public TestFixture { TEST_CASE(valueType2); TEST_CASE(crash); + TEST_CASE(nullNodeInvalidLevel); + } + + void nullNodeInvalidLevel() { + // a "<<>>" line whose indentation maps to level 0 must not index tree[-1] + const char* clang = "`-FunctionDecl 0x1 col:6 foo 'void ()'\n" + "`-<<>>\n"; + ASSERT_EQUALS("void foo ( ) ;", parse(clang)); } std::string parse(const char clang[]) {