Skip to content

Commit 5a3579b

Browse files
authored
Fix #15097 (clang import: oob tree index in parseClangAstDump on stray NULL node) (#8903)
1 parent b8e62df commit 5a3579b

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎lib/clangimport.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,9 @@ void clangimport::parseClangAstDump(Tokenizer &tokenizer, std::istream &f)
16371637
if (pos1 == std::string::npos)
16381638
continue;
16391639
if (!tree.empty() && line.substr(pos1) == "-<<<NULL>>>") {
1640-
const int level = (pos1 - 1) / 2;
1640+
const size_t level = (pos1 - 1) / 2;
1641+
if (level == 0 || level > tree.size())
1642+
continue;
16411643
tree[level - 1]->children.push_back(nullptr);
16421644
continue;
16431645
}

‎test/testclangimport.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class TestClangImport : public TestFixture {
140140

141141
TEST_CASE(crash);
142142
TEST_CASE(crash2);
143+
TEST_CASE(nullNodeInvalidLevel);
143144
}
144145

145146
std::string parse(const char clang[]) {
@@ -1380,6 +1381,13 @@ class TestClangImport : public TestFixture {
13801381
// out of bounds.
13811382
(void)parse("`-RecordDecl "); // don't crash
13821383
}
1384+
1385+
void nullNodeInvalidLevel() {
1386+
// a "<<<NULL>>>" line whose indentation maps to level 0 must not index tree[-1]
1387+
const char* clang = "`-FunctionDecl 0x1 <a.cpp:1:1, col:34> col:6 foo 'void ()'\n"
1388+
"`-<<<NULL>>>\n";
1389+
ASSERT_EQUALS("void foo ( ) ;", parse(clang));
1390+
}
13831391
};
13841392

13851393
REGISTER_TEST(TestClangImport)

0 commit comments

Comments
 (0)