Skip to content

Commit 4fb0899

Browse files
committed
fix oob tree index in parseClangAstDump on stray NULL node
1 parent a294079 commit 4fb0899

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
@@ -1639,7 +1639,9 @@ void clangimport::parseClangAstDump(Tokenizer &tokenizer, std::istream &f)
16391639
if (pos1 == std::string::npos)
16401640
continue;
16411641
if (!tree.empty() && line.substr(pos1) == "-<<<NULL>>>") {
1642-
const int level = (pos1 - 1) / 2;
1642+
const size_t level = (pos1 - 1) / 2;
1643+
if (level == 0 || level > tree.size())
1644+
continue;
16431645
tree[level - 1]->children.push_back(nullptr);
16441646
continue;
16451647
}

‎test/testclangimport.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ class TestClangImport : public TestFixture {
139139
TEST_CASE(valueType2);
140140

141141
TEST_CASE(crash);
142+
TEST_CASE(nullNodeInvalidLevel);
143+
}
144+
145+
void nullNodeInvalidLevel() {
146+
// a "<<<NULL>>>" line whose indentation maps to level 0 must not index tree[-1]
147+
const char* clang = "`-FunctionDecl 0x1 <a.cpp:1:1, col:34> col:6 foo 'void ()'\n"
148+
"`-<<<NULL>>>\n";
149+
ASSERT_EQUALS("void foo ( ) ;", parse(clang));
142150
}
143151

144152
std::string parse(const char clang[]) {

0 commit comments

Comments
 (0)