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

    <tr>
        <th>Summary</th>
        <td>
            clang-format fails with assertion "`idx < size()' failed"
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          ksdd
      </td>
    </tr>
</table>

<pre>
    
The archive [files.tar.gz](https://github.com/llvm/llvm-project/files/13810489/files.tar.gz) contains a "_clang-format" configuration file as well as a minimal example C file "crash-example.c". The provided C file compiles both with recent gcc and clang, although both submit some warnings (depending on options). The problem is reproducible with several recent versions of clang-format as well as with a build from latest main branch.

How to reproduce:

Unpack the archive [files.tar.gz](https://github.com/llvm/llvm-project/files/13810489/files.tar.gz) and run:

```
$ tar xzf files.tar.gz
$ clang-format --version
clang-format version 18.0.0git (https://github.com/llvm/llvm-project.git 2eb0ac0b3e3c74875e9b376239a27b8eb389189c)
$ clang-format files/crash-example.c
clang-format: /home/foo/llvm/include/llvm/ADT/SmallVector.h:298: const T& llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::operator[](llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type) const [with T = clang::format::WhitespaceManager::Change; <template-parameter-1-2> = void; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::const_reference = const clang::format::WhitespaceManager::Change&; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type = long unsigned int]: Assertion `idx < size()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: clang-format files/crash-example.c
 #0 0x000055accecb5bfe llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/foo/llvm/lib/Support/Unix/Signals.inc:723:22
 #1 0x000055accecb600f PrintStackTraceSignalHandler(void*) /home/foo/llvm/lib/Support/Unix/Signals.inc:798:1
 #2 0x000055accecb359a llvm::sys::RunSignalHandlers() /home/foo/llvm/lib/Support/Signals.cpp:105:20
 #3 0x000055accecb54b8 SignalHandler(int) /home/foo/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x00007f0cbce16420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #5 0x00007f0cbc8cf00b raise /build/glibc-wuryBv/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #6 0x00007f0cbc8ae859 abort /build/glibc-wuryBv/glibc-2.31/stdlib/abort.c:81:7
 #7 0x00007f0cbc8ae729 get_sysdep_segment_value /build/glibc-wuryBv/glibc-2.31/intl/loadmsgcat.c:509:8
 #8 0x00007f0cbc8ae729 _nl_load_domain /build/glibc-wuryBv/glibc-2.31/intl/loadmsgcat.c:970:34
 #9 0x00007f0cbc8bffd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
#10 0x000055accee397f9 llvm::SmallVectorTemplateCommon<clang::format::WhitespaceManager::Change, void>::operator[](unsigned long) const /home/foo/llvm/include/llvm/ADT/SmallVector.h:299:19
#11 0x000055accee39020 auto clang::format::WhitespaceManager::getNetWidth<clang::format::WhitespaceManager::CellDescription*>(clang::format::WhitespaceManager::CellDescription* const&, clang::format::WhitespaceManager::CellDescription* const&, unsigned int) const /home/foo/clang/lib/Format/WhitespaceManager.h:285:36
#12 0x000055accee39a59 unsigned int clang::format::WhitespaceManager::getMaximumNetWidth<clang::format::WhitespaceManager::CellDescription*>(clang::format::WhitespaceManager::CellDescription* const&, clang::format::WhitespaceManager::CellDescription* const&, unsigned int, unsigned int, unsigned int) const /home/foo/clang/lib/Format/WhitespaceManager.h:325:19
#13 0x000055accee2d617 clang::format::WhitespaceManager::alignArrayInitializersRightJustified(clang::format::WhitespaceManager::CellDescriptions&&) /home/foo/clang/lib/Format/WhitespaceManager.cpp:1310:46
#14 0x000055accee2d2a6 clang::format::WhitespaceManager::alignArrayInitializers(unsigned int, unsigned int) /home/foo/clang/lib/Format/WhitespaceManager.cpp:1272:50
#15 0x000055accee2d1f2 clang::format::WhitespaceManager::alignArrayInitializers() /home/foo/clang/lib/Format/WhitespaceManager.cpp:1258:23
#16 0x000055accee2ac51 clang::format::WhitespaceManager::generateReplacements() /home/foo/clang/lib/Format/WhitespaceManager.cpp:118:18
#17 0x000055acced6db4b clang::format::(anonymous namespace)::Formatter::analyze(clang::format::TokenAnnotator&, llvm::SmallVectorImpl<clang::format::AnnotatedLine*>&, clang::format::FormatTokenLexer&) /home/foo/clang/lib/Format/Format.cpp:2441:59
#18 0x000055accedcb2b6 clang::format::TokenAnalyzer::process(bool) /home/foo/clang/lib/Format/TokenAnalyzer.cpp:132:5
#19 0x000055acced7383f clang::format::internal::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus*)::'lambda9'(clang::format::Environment const&)::operator()(clang::format::Environment const&) const /home/foo/clang/lib/Format/Format.cpp:3730:12
#20 0x000055acced7ca16 std::_Function_handler<std::pair<clang::tooling::Replacements, unsigned int> (clang::format::Environment const&), clang::format::internal::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus*)::'lambda9'(clang::format::Environment const&)>::_M_invoke(std::_Any_data const&, clang::format::Environment const&) /usr/include/c++/9/bits/std_function.h:287:7
#21 0x000055acced943da std::function<std::pair<clang::tooling::Replacements, unsigned int> (clang::format::Environment const&)>::operator()(clang::format::Environment const&) const /usr/include/c++/9/bits/std_function.h:689:5
#22 0x000055acced7456d clang::format::internal::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus*) /home/foo/clang/lib/Format/Format.cpp:3746:49
#23 0x000055acced74b46 clang::format::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, llvm::StringRef, clang::format::FormattingAttemptStatus*) /home/foo/clang/lib/Format/Format.cpp:3788:28
#24 0x000055accebbcbfd clang::format::format(llvm::StringRef, bool) /home/foo/clang/tools/clang-format/ClangFormat.cpp:499:76
#25 0x000055accebbe24d main /home/foo/clang/tools/clang-format/ClangFormat.cpp:692:11
#26 0x00007f0cbc8b0083 __libc_start_main /build/glibc-wuryBv/glibc-2.31/csu/../csu/libc-start.c:342:3
#27 0x000055accebbaa4e _start (/home/foo/build/bin/clang-format+0x349a4e)
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWltv2zgW_jXMCxGDInV9yINzMWYW7aJoMzuPAkVRMrcUKZBULv31C1KyZbl1GzcZdIEZoGgiUeL5znfuVKi1olWcX4HkGiS3F3RwW22uPtu6vqh0_XwF0C1A6_sth9SwrXjgECTXjZDcrhw1q_YLSG4BzrfO9RaQNcAbgDetcNuhWjHdAbyR8mH347I3-r-cOYA3YQuANxHJIxTnxe7WbldcQKaVo0JZSCHAuGSSqvay0aajDmDslxvRDoY6oRX0L0Nq4SOX0v-ksBNKdFRC_kS7XnJ4Mz4DMGaG2u3ldH_FAMYr6DXsjX4QNa93jzLd9R4TrLTbwkfhttBwxpWDLWOQqhoGTADfQCrdVg_tdnzUDlUnHLS64_CRGiVUayHAec17rmqhWqgV1L0HbgEu9tIryTsoLDS8N7oemKgkH-Va_sANlTv5D9xY_zLUDTzk5ZCB8B6F1SBkDRujOyip49bBjgoFK0MV265G847__6YfodN74dyb82D5D9VT9hm6X-EKnmszqCNEIEXTv_ESx9BRA5--NHCxwX51QdXl5cTiuL5Ym1ZglK_QCrXCwbMVW_m3MK8QZaginLAszrOEFxXJUkwKirMq5xXJiygvGMDFCZQ7co599ivMgKwhwJut7rgnUOsZllBMDjWfb6xv7wHefOqolP_hzGmz2gKyxkXuN2FaWQfvAU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