Skip to content

Commit

Permalink
[clang-format] Fix use after free in FormatTokenSource test
Browse files Browse the repository at this point in the history
Add a lifetime annotation to IndexedTokenSource so we get a warning for
this pattern.
  • Loading branch information
d0k committed Jan 31, 2023
1 parent eb6f5f1 commit ffc9b80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions clang/lib/Format/FormatTokenSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FormatTokenSource {
virtual FormatToken *setPosition(unsigned Position) = 0;
};

class IndexedTokenSource : public FormatTokenSource {
class LLVM_GSL_POINTER IndexedTokenSource : public FormatTokenSource {
public:
IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
: Tokens(Tokens), Position(-1) {}
Expand Down Expand Up @@ -198,4 +198,4 @@ class ScopedMacroState : public FormatTokenSource {

#undef DEBUG_TYPE

#endif
#endif
11 changes: 7 additions & 4 deletions clang/unittests/Format/FormatTokenSourceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class IndexedTokenSourceTest : public ::testing::Test {
} while (false);

TEST_F(IndexedTokenSourceTest, EmptyInput) {
IndexedTokenSource Source(lex(""));
TokenList Tokens = lex("");
IndexedTokenSource Source(Tokens);
EXPECT_FALSE(Source.isEOF());
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TRUE(Source.isEOF());
Expand All @@ -45,7 +46,8 @@ TEST_F(IndexedTokenSourceTest, EmptyInput) {
}

TEST_F(IndexedTokenSourceTest, NavigateTokenStream) {
IndexedTokenSource Source(lex("int a;"));
TokenList Tokens = lex("int a;");
IndexedTokenSource Source(Tokens);
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::kw_int);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::kw_int);
EXPECT_EQ(Source.getPreviousToken(), nullptr);
Expand All @@ -61,7 +63,8 @@ TEST_F(IndexedTokenSourceTest, NavigateTokenStream) {
}

TEST_F(IndexedTokenSourceTest, ResetPosition) {
IndexedTokenSource Source(lex("int a;"));
TokenList Tokens = lex("int a;");
IndexedTokenSource Source(Tokens);
Source.getNextToken();
unsigned Position = Source.getPosition();
Source.getNextToken();
Expand All @@ -72,4 +75,4 @@ TEST_F(IndexedTokenSourceTest, ResetPosition) {

} // namespace
} // namespace format
} // namespace clang
} // namespace clang

0 comments on commit ffc9b80

Please sign in to comment.