Skip to content

Commit b8e62df

Browse files
authored
fix oob read in getSpelling when node has no ext tokens (cppcheck-opensource#8907)
1 parent 2a9e809 commit b8e62df

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

‎lib/clangimport.cpp‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,13 @@ std::string clangimport::AstNode::getSpelling() const
422422
if (nodeType == FunctionDecl || nodeType == CXXConstructorDecl || nodeType == CXXMethodDecl) {
423423
while (typeIndex >= 0 && mExtTokens[typeIndex][0] != '\'')
424424
typeIndex--;
425-
if (typeIndex <= 0)
426-
return "";
427425
}
428426
if (nodeType == DeclRefExpr) {
429427
while (typeIndex > 0 && std::isalpha(mExtTokens[typeIndex][0]))
430428
typeIndex--;
431-
if (typeIndex <= 0)
432-
return "";
433429
}
430+
if (typeIndex <= 0)
431+
return "";
434432
const std::string &str = mExtTokens[typeIndex - 1];
435433
if (startsWith(str,"col:"))
436434
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)