Issue 208678
Summary [clang][ObjC++] Segfault in Parser::StashAwayMethodOrFunctionBodyTokens when parsing @implementation containing extern "C" block with nested @implementation
Labels
Assignees
Reporter YuanchengJiang
    **Description**

`clang++` crashes with a segmentation fault in `clang::Parser::StashAwayMethodOrFunctionBodyTokens` when parsing an Objective-C++ file where an `@implementation` block contains an `extern "C"` block with a nested `@interface`/`@implementation`, followed by a method definition and a trailing `@end`.

**Reproducer**

```objc
@interface ISDPropertyChangeGroup
@end

@implementation ISDPropertyChangeGroup

extern "C" {
@interface QQ
@end

@implementation QQ
@end
}

- (id)lastModifiedGeneration : (id) obj
{
  return obj;
}
;
@end
```

**Command**

```
clang++ reproduce.mm
```

**Actual behavior**

```
fatal error: too many errors emitted, stopping now [-ferror-limit=]
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: clang -cc1 ... -x objective-c++ reproduce.mm
1. reproduce.mm:XX:1: current parser token '{'
2. reproduce.mm:XX:1: parsing Objective-C method 'ISDPropertyChangeGroup::lastModifiedGeneration:'
 #0 llvm::sys::PrintStackTrace(...)
 #4 clang::Parser::StashAwayMethodOrFunctionBodyTokens(clang::Decl*)
 #5 clang::Parser::ParseObjCMethodDefinition()
 #6 clang::Parser::ParseExternalDeclaration(...)
 #7 clang::Parser::ParseObjCAtImplementationDeclaration(...)
...
clang++: error: unable to execute command: Segmentation fault (core dumped)
clang++: error: clang frontend command failed due to signal
```

**Expected behavior**

The compiler should emit error diagnostics about the malformed Objective-C++ constructs and exit gracefully. A segfault in the parser is never acceptable regardless of how invalid the input is.

**Environment**

- Compiler: Debian clang version 21.1.8 (20251221033036+2078da43e25a)
- Target: x86_64-pc-linux-gnu
- Command: `clang++ reproduce.mm` (no special flags required)
- Crash site: `clang::Parser::StashAwayMethodOrFunctionBodyTokens` in `libclang-cpp.so`
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to