<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64701>64701</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-format] Segmentation fault when formatting nested namespaces
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
d0nc1h0t
</td>
</tr>
</table>
<pre>
When formatting this part of the code:
```
namespace ns1 { namespace ns2 { namespace ns3 {
void func_in_ns() {
int res{ 0 };
// block for debug mode
#ifndef NDEBUG
#endif
}
}}}
```
with the following style settings:
```
---
Language: Cpp
BasedOnStyle: LLVM
CompactNamespaces: true
NamespaceIndentation: Inner
```
clang-format crashes with an error segmentation fault. As I found out, this is due to the overflow of the Level value for the comment line. Overflow occurs in line 393 of the file clang/lib/Format/UnwrappedLineFormatter.cpp:
```
...
if (!(*CompactedLine)->InPPDirective)
(*CompactedLine)->Level -= OutdentBy;
...
```
OutdentBy turns out to be greater than the value (*CompactedLine)->Level, which causes an overflow.
I see the following way to solve the problem:
```
...
if (!(*CompactedLine)->InPPDirective)
(*CompactedLine)->Level -= std:min(OutdentBy, (*CompactedLine)->Level);
...
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0lV-PmzgQwD-N8zIKAhOS8MDDZmlOkXJtpap3j5UxA_jO2Mh_Eu23P9mQTbrbVZ8OISeescczvxkPzFrRK8SKFAdS1Cvm3aBN1aaKZ0PqVo1uX6q_B1TQaTMy54TqwQ3CwsSMA92BGxC4bpHkTyStSfpEtunyxqliI9qJcQRlMyC7AzxK6DtJHiSLpThetGih84r_EOqHsoTuCS3viwAAhHJg0AZTKZBdTfKbBXok9AiN1PzfEAG02PgexuDvsiAXnWqxg8_1p8P3Px4PJjRH1Ypume3qn5S7enl_FfQ8XoUbIp9OS6mvAZ11LxLBYgRpP2K2Xq_nP2emes_6ABeW53maZt2BWWy_qG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