Skip to content

Commit a3c8020

Browse files
committed
fix oob read in getSpelling when node has no ext tokens
1 parent fa6c1f1 commit a3c8020

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎lib/clangimport.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ std::string clangimport::AstNode::getSpelling() const
431431
if (typeIndex <= 0)
432432
return "";
433433
}
434+
if (typeIndex <= 0)
435+
return "";
434436
const std::string &str = mExtTokens[typeIndex - 1];
435437
if (startsWith(str,"col:"))
436438
return "";

‎test/testclangimport.cpp‎

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

141141
TEST_CASE(crash);
142+
TEST_CASE(crash2);
142143
}
143144

144145
std::string parse(const char clang[]) {
@@ -1372,6 +1373,13 @@ class TestClangImport : public TestFixture {
13721373
" `-CompoundStmt 0x5603791b5700 <col:54, col:55>\n";
13731374
(void)parse(clang); // don't crash
13741375
}
1376+
1377+
void crash2() {
1378+
// getSpelling() indexed mExtTokens[typeIndex - 1] without a lower-bound
1379+
// check, so a node whose line carries no ext tokens (typeIndex <= 0) read
1380+
// out of bounds.
1381+
(void)parse("`-RecordDecl "); // don't crash
1382+
}
13751383
};
13761384

13771385
REGISTER_TEST(TestClangImport)

0 commit comments

Comments
 (0)