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

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 19 10:27:43 PDT 2024


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/113056

Fixes #113011.

>From dd3bba3760b74806871593c430f9c1f6b55cfe62 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 19 Oct 2024 10:25:32 -0700
Subject: [PATCH] [clang-format] Fix a crash on C# `goto case`

Fixes #113011.
---
 clang/lib/Format/UnwrappedLineParser.cpp    |  2 +-
 clang/unittests/Format/FormatTestCSharp.cpp | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

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