Skip to content

[clang] Bounds checking on unclosed parentheses, brackets or braces in Expanded Tokens #69849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 19 additions & 6 deletions clang/lib/Tooling/Syntax/Tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,13 @@ TokenBuffer::spelledForExpandedToken(const syntax::Token *Expanded) const {
});
// Our token could only be produced by the previous mapping.
if (It == File.Mappings.begin()) {
// No previous mapping, no need to modify offsets.
return {&File.SpelledTokens[ExpandedIndex - File.BeginExpanded],
/*Mapping=*/nullptr};
const auto offset = ExpandedIndex - File.BeginExpanded;
// Bounds check so parsing an unclosed parenthesis, brackets or braces do
// not result in UB.
if (offset >= File.SpelledTokens.size()) {
return {nullptr, nullptr};
}
return {&File.SpelledTokens[offset], /*Mapping=*/nullptr};
}
--It; // 'It' now points to last mapping that started before our token.

Expand All @@ -298,9 +302,12 @@ TokenBuffer::spelledForExpandedToken(const syntax::Token *Expanded) const {

// Not part of the mapping, use the index from previous mapping to compute the
// corresponding spelled token.
return {
&File.SpelledTokens[It->EndSpelled + (ExpandedIndex - It->EndExpanded)],
/*Mapping=*/nullptr};
const auto offset = It->EndSpelled + (ExpandedIndex - It->EndExpanded);
// This index can also result in UB when parsing unclosed tokens.
if (offset >= File.SpelledTokens.size()) {
return {nullptr, nullptr};
}
return {&File.SpelledTokens[offset], /*Mapping=*/nullptr};
}

const TokenBuffer::Mapping *
Expand Down Expand Up @@ -410,6 +417,12 @@ TokenBuffer::spelledForExpanded(llvm::ArrayRef<syntax::Token> Expanded) const {
auto [FirstSpelled, FirstMapping] = spelledForExpandedToken(First);
auto [LastSpelled, LastMapping] = spelledForExpandedToken(Last);

// If there was an unclosed token, FirstSpelled or LastSpelled is null and the
// operation shouldn't continue.
if (FirstSpelled == nullptr || LastSpelled == nullptr) {
return std::nullopt;
};

FileID FID = SourceMgr->getFileID(FirstSpelled->location());
// FIXME: Handle multi-file changes by trying to map onto a common root.
if (FID != SourceMgr->getFileID(LastSpelled->location()))
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Tooling/Syntax/TokensTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,17 @@ TEST_F(TokenCollectorTest, DelayedParsing) {
EXPECT_THAT(collectAndDump(Code), StartsWith(ExpectedTokens));
}

TEST_F(TokenCollectorTest, UnclosedToken) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I started looking at this to try and make some progress towards getting this landed. However, when running this test locally, I'm not seeing either of the branches added to spelledForExpandedToken() being taken.

Am I missing something, or is this test not exercising the intended codepath?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, it looks like TokenBuffer::spelledForExpanded() (which is the only call site of spelledForExpandedToken()) is not called by anything currently in this test.

To exercise spelledForExpanded(), we will need to make an explicit call to it, the way a number of other tests in this file do.

llvm::StringLiteral Code = R"cpp(
int main() {
// this should not result in a segfault or UB.
)cpp";
std::string ExpectedTokens =
"expanded tokens:\n int main ( ) {\nfile './input.cpp'\n spelled "
"tokens:\n int main ( ) {\n no mappings.\n";
EXPECT_THAT(collectAndDump(Code), StartsWith(ExpectedTokens));
}

TEST_F(TokenCollectorTest, MultiFile) {
addFile("./foo.h", R"cpp(
#define ADD(X, Y) X+Y
Expand Down