[clang][ObjC++] Segfault in Parser::StashAwayMethodOrFunctionBodyTokens when parsing @implementation containing extern "C" block with nested @implementation
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
**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`
Contributor guide
Research direction
Reproduce the crash with the provided Objective-C++ source using `clang++ reproduce.mm`, then inspect `clang::Parser::StashAwayMethodOrFunctionBodyTokens` and the surrounding Objective-C method parsing path. Done means the malformed nested constructs produce diagnostics and clang exits without a segmentation fault; the payload does not name a regression test file.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, objective-c
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100