Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,13 @@ std::string clangimport::AstNode::getSpelling() const
if (nodeType == FunctionDecl || nodeType == CXXConstructorDecl || nodeType == CXXMethodDecl) {
while (typeIndex >= 0 && mExtTokens[typeIndex][0] != '\'')
typeIndex--;
if (typeIndex <= 0)
return "";
}
if (nodeType == DeclRefExpr) {
while (typeIndex > 0 && std::isalpha(mExtTokens[typeIndex][0]))
typeIndex--;
if (typeIndex <= 0)
return "";
}
if (typeIndex <= 0)
return "";
Comment on lines +430 to +431

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.

The fix looks correct. I verified that the new crash2 test fails on main (built with -D_GLIBCXX_ASSERTIONS: Assertion '__n < this->size()' failed in getSpelling()) and passes with this PR.

The description says the guard is hoisted, but the two existing if (typeIndex <= 0) return ""; checks in the FunctionDecl/CXXConstructorDecl/CXXMethodDecl and DeclRefExpr branches above are kept. They are now redundant, since this new check covers them. Maybe remove them so there is only one guard? I tried that locally and TestClangImport still passes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, those two are redundant now. Dropped both so there's just the single guard before the index. TestClangImport still passes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve button

Thanks, the update looks good to me.

FYI: #8903 also adds a test right after crash() and a TEST_CASE line in the same place in test/testclangimport.cpp, so whichever of the two PRs is merged second will get a trivial merge conflict.

const std::string &str = mExtTokens[typeIndex - 1];
if (startsWith(str,"col:"))
return "";
Expand Down
8 changes: 8 additions & 0 deletions test/testclangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class TestClangImport : public TestFixture {
TEST_CASE(valueType2);

TEST_CASE(crash);
TEST_CASE(crash2);
}

std::string parse(const char clang[]) {
Expand Down Expand Up @@ -1372,6 +1373,13 @@ class TestClangImport : public TestFixture {
" `-CompoundStmt 0x5603791b5700 <col:54, col:55>\n";
(void)parse(clang); // don't crash
}

void crash2() {
// getSpelling() indexed mExtTokens[typeIndex - 1] without a lower-bound
// check, so a node whose line carries no ext tokens (typeIndex <= 0) read
// out of bounds.
(void)parse("`-RecordDecl "); // don't crash
}
};

REGISTER_TEST(TestClangImport)