<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/74281>74281</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] carsh on invalid code when parsing struct body
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jun-wei-zeng
      </td>
    </tr>
</table>

<pre>
    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)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWl9zm7gW_zTkRSMPCAzmIQ-uHXcztzvtNLm7jxkhHWxtQeJKInH309-RwA4Qx0m7vjt7O7WDhXT0Oz-dfwJRY8RWAlwH8w_BfH1FW7tT-vqPVuInEPhPkNurQvHv1ytVN6ICjWDfVEqDDuIl2lnbmCBeBmQTkM1W8UJVdqb0NiCbPwOyiUF_LG5K_WsQroNw2X3f7wCVqqrUk5BbJOQjrQRHTHFATFOzA4OediAR62bkqDWuI6uo3M7Q_U4YVLRbZABqg6xCO_oIqACQqNFgQFpkhGSAgjT0Y3A8WwRpOBtiCNKw-8-63zQgi4DkKMg-dA0IGatbZlExbHtuDeJjY5CtT3Q-zjCcduX0Qxoapa2j7RQis4Oq6tvilVGtZhDEN0G8jPoPAq2V599-bwCZBpgoBWhUC-OoCsgKcShpW1lPUEAyIW1AsiD-gG7vPqNVniMqOaqoBY24QlJZZNrGwUKibirBhEVCWhTMP-DfDy3YSZmvX2EsmN90F67XKfhxEC_nDvUT1dLBjJeIA6uoplYoibgC45F0jYCo_G53buk9il45PBhijmhOrQtCR0gTJEn_eSYS9g0wCxx1LAUkQ7R05HRyzy_1cCZ3fYBwYtZo_vq02dp9vzXT4N859SKvn1QWvJ0oVFPLdsg67_GTfXie7B1zHaf68ulmeXeDTFvUwiLqPbGzZ--K43gg7K4tZkzVAdlU1ePhD260-gOYDchGGNOCCcjG26OQrGo5ILvrQwEqKPtmN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