Issue 64701
Summary [clang-format] Segmentation fault when formatting nested namespaces
Labels new issue
Assignees
Reporter d0nc1h0t
    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);
 ...
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to