[clang] b5bcdb5 - [clang-format] Fix a crash on C# `goto case` (#113056)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 21 18:50:18 PDT 2024


Author: Owen Pan
Date: 2024-10-21T18:50:13-07:00
New Revision: b5bcdb5cfae452da30a699d7967cc1e0800ca997

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

LOG: [clang-format] Fix a crash on C# `goto case` (#113056)

Fixes #113011.

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/FormatTestCSharp.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index bda9850670ab06..4a5109983dfcc5 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2148,7 +2148,7 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
   if (!Style.isCSharp())
     return false;
   // See if it's a property accessor.
-  if (FormatTok->Previous->isNot(tok::identifier))
+  if (!FormatTok->Previous || FormatTok->Previous->isNot(tok::identifier))
     return false;
 
   // See if we are inside a property accessor.

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 3b04238b9b48b0..0c5afa1524374d 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -1688,6 +1688,18 @@ TEST_F(FormatTestCSharp, BrokenBrackets) {
   EXPECT_NE("", format("int where b <")); // reduced from crasher
 }
 
+TEST_F(FormatTestCSharp, GotoCaseLabel) {
+  verifyNoCrash("switch (i) {\n"
+                "case 0:\n"
+                "  goto case 1;\n"
+                "case 1:\n"
+                "  j = 0;\n"
+                "  {\n"
+                "    break;\n"
+                "  }\n"
+                "}");
+}
+
 } // namespace
 } // namespace test
 } // namespace format


        


More information about the cfe-commits mailing list