Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Segmentation fault when formatting nested namespaces #64701

Closed
d0nc1h0t opened this issue Aug 15, 2023 · 3 comments
Closed

[clang-format] Segmentation fault when formatting nested namespaces #64701

d0nc1h0t opened this issue Aug 15, 2023 · 3 comments

Comments

@d0nc1h0t
Copy link
Contributor

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);
    ...
@llvmbot
Copy link
Collaborator

llvmbot commented Aug 15, 2023

@llvm/issue-subscribers-clang-format

@owenca
Copy link
Contributor

owenca commented Aug 18, 2023

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);
    ...

This seems to work. Can you submit a patch at https://reviews.llvm.org?

Tagging @rymiel.

@d0nc1h0t
Copy link
Contributor Author

Submit patch D158363.

@owenca owenca closed this as completed in f465a48 Sep 5, 2023
avillega pushed a commit to avillega/llvm-project that referenced this issue Sep 11, 2023
Fixing the clang-format crash with the segmentation fault error when
formatting code with nested namespaces.

Fixes llvm#64701.

Differential Revision: https://reviews.llvm.org/D158363
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants