diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index f9520aba4b2..dc549e19df6 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -1637,7 +1637,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 229e4c3c982..c007a4806da 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -140,6 +140,7 @@ class TestClangImport : public TestFixture { TEST_CASE(crash); TEST_CASE(crash2); + TEST_CASE(nullNodeInvalidLevel); } std::string parse(const char clang[]) { @@ -1380,6 +1381,13 @@ class TestClangImport : public TestFixture { // out of bounds. (void)parse("`-RecordDecl "); // don't crash } + + 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)); + } }; REGISTER_TEST(TestClangImport)