[clang] 341dd60 - [Lex] Fix a crash in updateConsecutiveMacroArgTokens.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 22 01:16:30 PST 2023


Author: Haojian Wu
Date: 2023-02-22T10:16:02+01:00
New Revision: 341dd6076b123946f79a3148b660d6579f9683a7

URL: https://github.com/llvm/llvm-project/commit/341dd6076b123946f79a3148b660d6579f9683a7
DIFF: https://github.com/llvm/llvm-project/commit/341dd6076b123946f79a3148b660d6579f9683a7.diff

LOG: [Lex] Fix a crash in updateConsecutiveMacroArgTokens.

Fixes https://github.com/llvm/llvm-project/issues/60722.

Differential Revision: https://reviews.llvm.org/D144054

Added: 
    clang/test/Lexer/update_consecutive_macro_crash.cpp

Modified: 
    clang/lib/Lex/TokenLexer.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/TokenLexer.cpp b/clang/lib/Lex/TokenLexer.cpp
index e0cd77b0db8f0..856d5682727fe 100644
--- a/clang/lib/Lex/TokenLexer.cpp
+++ b/clang/lib/Lex/TokenLexer.cpp
@@ -1019,8 +1019,16 @@ static void updateConsecutiveMacroArgTokens(SourceManager &SM,
     SourceLocation Limit =
         SM.getComposedLoc(BeginFID, SM.getFileIDSize(BeginFID));
     Partition = All.take_while([&](const Token &T) {
-      return T.getLocation() >= BeginLoc && T.getLocation() < Limit &&
-             NearLast(T.getLocation());
+      // NOTE: the Limit is included! The lexer recovery only ever inserts a
+      // single token past the end of the FileID, specifically the ) when a
+      // macro-arg containing a comma should be guarded by parentheses.
+      //
+      // It is safe to include the Limit here because SourceManager allocates
+      // FileSize + 1 for each SLocEntry.
+      //
+      // See https://github.com/llvm/llvm-project/issues/60722.
+      return T.getLocation() >= BeginLoc && T.getLocation() <= Limit
+         &&  NearLast(T.getLocation());
     });
   }
   assert(!Partition.empty());

diff  --git a/clang/test/Lexer/update_consecutive_macro_crash.cpp b/clang/test/Lexer/update_consecutive_macro_crash.cpp
new file mode 100644
index 0000000000000..c66e734a4894f
--- /dev/null
+++ b/clang/test/Lexer/update_consecutive_macro_crash.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang -cc1 -fsyntax-only -verify %s 2>&1
+
+#define X(val2) Y(val2++) // expected-note {{macro 'X' defined here}}
+#define Y(expression) expression ;
+
+void foo() {
+  // https://github.com/llvm/llvm-project/issues/60722:
+  //
+  // - Due to to the error recovery, the lexer inserts a pair of () around the
+  //   macro argument int{,}, so we will see [(, int, {, ,, }, )] tokens.
+  // - however, the size of file id for the macro argument only takes account
+  //   the written tokens  int{,} , and the extra inserted ) token points to the
+  //    Limit source location which triggered an empty Partition violation.
+  X(int{,}); // expected-error {{too many arguments provided to function-like macro invocation}} \
+                 expected-error {{expected expression}} \
+                 expected-note {{parentheses are required around macro argument containing braced initializer list}}
+}


        


More information about the cfe-commits mailing list