Issue 74281
Summary [clang] carsh on invalid code when parsing struct body
Labels clang
Assignees
Reporter jun-wei-zeng
    Compiler explorer: https://godbolt.org/z/3erGbEfrM

The following invalid code crashes when compiled using clang. This bug seems to have been present since `clang-3.8`.

```c
a() {
  struct b {
    struct b;
  } struct b {
```

Crash report:

```shell
<source>:1:1: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
a() {
^
int
<source>:3:5: warning: declaration does not declare anything [-Wmissing-declarations]
 struct b;
    ^
<source>:4:4: error: expected ';' after struct
  } struct b {
   ^
   ;
<source>:4:15: error: expected '}'
  } struct b {
              ^
<source>:4:14: note: to match this '{'
 } struct b {
             ^
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: /opt/compiler-explorer/clang-16.0.0/bin/clang-16 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name example.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-16.0.0/lib/clang/16 -internal-isystem /opt/compiler-explorer/clang-16.0.0/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir=/app -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/example-1992d9.o -x c <source>
1.	<eof> parser at end of file
2.	<source>:1:5: parsing function body 'a'
3.	<source>:1:5: in compound statement ('{}')
4.	<source>:4:5: parsing struct/union body
 #0 0x00005564cde1a8df llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x3ba18df)
 #1 0x00005564cde18354 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f3219842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00005564d12d4f34 clang::RecordDecl::field_begin() const (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x705bf34)
 #4 0x00005564d036a2b8 clang::Parser::ParseStructUnionBody(clang::SourceLocation, clang::TypeSpecifierType, clang::RecordDecl*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60f12b8)
 #5 0x00005564d0390ab1 clang::Parser::ParseClassSpecifier(clang::tok::TokenKind, clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, bool, clang::Parser::DeclSpecContext, clang::ParsedAttributes&) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x6117ab1)
 #6 0x00005564d036201e clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*, clang::ImplicitTypenameContext) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60e901e)
 #7 0x00005564d036e620 clang::Parser::ParseSimpleDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, bool, clang::Parser::ForRangeInit*, clang::SourceLocation*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60f5620)
 #8 0x00005564d036eb77 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60f5b77)
 #9 0x00005564d041bf0e clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x61a2f0e)
#10 0x00005564d041c7b3 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x61a37b3)
#11 0x00005564d041d666 clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x61a4666)
#12 0x00005564d041ea22 clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x61a5a22)
#13 0x00005564d033c174 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60c3174)
#14 0x00005564d036d2ff clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60f42ff)
#15 0x00005564d0336617 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60bd617)
#16 0x00005564d033775f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.0) Parser.cpp:0:0
#17 0x00005564d033f194 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60c6194)
#18 0x00005564d034072d clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60c772d)
#19 0x00005564d0340cc4 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60c7cc4)
#20 0x00005564d032fa5a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x60b6a5a)
#21 0x00005564cf10a865 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x4e91865)
#22 0x00005564ce9e6671 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x476d671)
#23 0x00005564ce96a993 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x46f1993)
#24 0x00005564ceacab3b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x4851b3b)
#25 0x00005564cb5f341c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x137a41c)
#26 0x00005564cb5eebdc ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#27 0x00005564cb5ef207 clang_main(int, char**) (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x1376207)
#28 0x00007f3219829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#29 0x00007f3219829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#30 0x00005564cb5ead1e _start (/opt/compiler-explorer/clang-16.0.0/bin/clang-16+0x1371d1e)
clang-16: error: unable to execute command: Segmentation fault (core dumped)
clang-16: error: clang frontend command failed due to signal (use -v to see invocation)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to