https://github.com/mygitljf updated https://github.com/llvm/llvm-project/pull/199104
>From e4eb2faec142d472d4e6e935e287f369e34334b8 Mon Sep 17 00:00:00 2001 From: mygitljf <[email protected]> Date: Thu, 21 May 2026 20:05:47 +0000 Subject: [PATCH 1/2] [clang-format][ObjC] Fix assertion crash on stray '-'/'+' in @interface body UnwrappedLineParser::parseObjCUntilAtEnd() consumes a leading '-' or '+' and unconditionally calls parseObjCMethod(), whose entry assertion requires the next token to be '(' or an identifier. Malformed input such as '@interface;\n-' violates that contract and aborts. Guard the call site so parseObjCMethod() is only entered when its entry contract is satisfied; the entry assertion itself is preserved. Stray '-'/'+' tokens fall through to the next iteration of the parseObjCUntilAtEnd loop and are absorbed by '@end' or EOF. Adds a regression test (FormatTestObjC.NoCrashOnStrayMethodSign) covering all variants reported in the issue. Fixes #199075 --- clang/lib/Format/UnwrappedLineParser.cpp | 6 +++++- clang/unittests/Format/FormatTestObjC.cpp | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 9536a233def58..3fb719c2a67a2 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -4262,7 +4262,11 @@ void UnwrappedLineParser::parseObjCUntilAtEnd() { addUnwrappedLine(); } else if (FormatTok->isOneOf(tok::minus, tok::plus)) { nextToken(); - parseObjCMethod(); + // Guard against malformed input where '-'/'+' is not followed by a + // method declaration. parseObjCMethod() asserts the next token is '(' + // or an identifier; silently skip the stray sign token otherwise. + if (FormatTok->isOneOf(tok::l_paren, tok::identifier)) + parseObjCMethod(); } else { parseStructuralElement(); } diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index 09a9687d6f87a..b6aa96f5aea79 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -1803,6 +1803,18 @@ TEST_F(FormatTestObjC, AttributesOnObjCProperty) { "@property(weak) id delegate ATTRIBUTE_MACRO(X) __attribute__((X));"); } +TEST_F(FormatTestObjC, NoCrashOnStrayMethodSign) { + // Issue #199075: clang-format used to assert in parseObjCMethod() when an + // ObjC interface/implementation contained a '-' or '+' that was not + // followed by '(' or an identifier. + verifyNoCrash("@interface;\n-"); + verifyNoCrash("@interface Foo\n-"); + verifyNoCrash("@interface Foo\n+"); + verifyNoCrash("@implementation Foo\n-"); + verifyNoCrash("@interface Foo\n-\n@end"); + verifyNoCrash("@interface Foo\n- ;"); +} + } // end namespace } // namespace test } // end namespace format >From 04ec6e8f279b22f0486e3e385c1adf1c88654c28 Mon Sep 17 00:00:00 2001 From: mygitljf <[email protected]> Date: Sat, 23 May 2026 08:52:55 +0000 Subject: [PATCH 2/2] addressing code review suggestions --- clang/lib/Format/UnwrappedLineParser.cpp | 3 --- clang/unittests/Format/FormatTestObjC.cpp | 22 +++++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 3fb719c2a67a2..c62c5996b0f12 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -4262,9 +4262,6 @@ void UnwrappedLineParser::parseObjCUntilAtEnd() { addUnwrappedLine(); } else if (FormatTok->isOneOf(tok::minus, tok::plus)) { nextToken(); - // Guard against malformed input where '-'/'+' is not followed by a - // method declaration. parseObjCMethod() asserts the next token is '(' - // or an identifier; silently skip the stray sign token otherwise. if (FormatTok->isOneOf(tok::l_paren, tok::identifier)) parseObjCMethod(); } else { diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index b6aa96f5aea79..a377c065aecc9 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -1804,15 +1804,19 @@ TEST_F(FormatTestObjC, AttributesOnObjCProperty) { } TEST_F(FormatTestObjC, NoCrashOnStrayMethodSign) { - // Issue #199075: clang-format used to assert in parseObjCMethod() when an - // ObjC interface/implementation contained a '-' or '+' that was not - // followed by '(' or an identifier. - verifyNoCrash("@interface;\n-"); - verifyNoCrash("@interface Foo\n-"); - verifyNoCrash("@interface Foo\n+"); - verifyNoCrash("@implementation Foo\n-"); - verifyNoCrash("@interface Foo\n-\n@end"); - verifyNoCrash("@interface Foo\n- ;"); + verifyNoCrash("@interface;\n" + "-"); + verifyNoCrash("@interface Foo\n" + "-"); + verifyNoCrash("@interface Foo\n" + "+"); + verifyNoCrash("@implementation Foo\n" + "-"); + verifyNoCrash("@interface Foo\n" + "-\n" + "@end"); + verifyNoCrash("@interface Foo\n" + "- ;"); } } // end namespace _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
