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..5da5ed70b68 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -139,6 +139,7 @@ class TestClangImport : public TestFixture { TEST_CASE(valueType2); TEST_CASE(crash); + TEST_CASE(nullNodeInvalidLevel); } std::string parse(const char clang[]) { @@ -1372,6 +1373,13 @@ class TestClangImport : public TestFixture { " `-CompoundStmt 0x5603791b5700 \n"; (void)parse(clang); // 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)