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] Stop crashing when formatting Verilog #112043

Merged
merged 2 commits into from
Oct 14, 2024

Conversation

sstwcw
Copy link
Contributor

@sstwcw sstwcw commented Oct 11, 2024

The part of the code for parsing Verilog module instantiations dereferenced a pointer without checking for null pointer. The pointer may be null if the input is not complete and a line starts with a comma.

The part of the code for parsing Verilog module instantiations
dereferenced a pointer without checking for null pointer.  The pointer
may be null if the input is not complete and a line starts with a comma.
@llvmbot
Copy link
Member

llvmbot commented Oct 11, 2024

@llvm/pr-subscribers-clang-format

Author: None (sstwcw)

Changes

The part of the code for parsing Verilog module instantiations dereferenced a pointer without checking for null pointer. The pointer may be null if the input is not complete and a line starts with a comma.


Full diff: https://github.com/llvm/llvm-project/pull/112043.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+2-1)
  • (modified) clang/unittests/Format/FormatTestVerilog.cpp (+1)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 364d7e9855e8cf..f41cf3b32f74e2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1551,7 +1551,8 @@ class AnnotatingParser {
           // Case D.
           if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
             const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
-            if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
+            if (PrevParen && PrevParen->is(tok::r_paren) &&
+                PrevParen->MatchingParen &&
                 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
               return true;
             }
diff --git a/clang/unittests/Format/FormatTestVerilog.cpp b/clang/unittests/Format/FormatTestVerilog.cpp
index fbaf289fbc4d6d..49d276fc78d81b 100644
--- a/clang/unittests/Format/FormatTestVerilog.cpp
+++ b/clang/unittests/Format/FormatTestVerilog.cpp
@@ -964,6 +964,7 @@ TEST_F(FormatTestVerilog, Instantiation) {
                "        .qbar(out1),\n"
                "        .clear(in1),\n"
                "        .preset(in2));");
+  verifyNoCrash(", ff1();");
   // With breaking between instance ports disabled.
   auto Style = getDefaultStyle();
   Style.VerilogBreakBetweenInstancePorts = false;

@sstwcw sstwcw merged commit 253ff32 into llvm:main Oct 14, 2024
5 of 6 checks passed
@sstwcw sstwcw deleted the format-verilog-crash branch October 14, 2024 13:57
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
The part of the code for parsing Verilog module instantiations
dereferenced a pointer without checking for null pointer. The pointer
may be null if the input is not complete and a line starts with a comma.
bricknerb pushed a commit to bricknerb/llvm-project that referenced this pull request Oct 17, 2024
The part of the code for parsing Verilog module instantiations
dereferenced a pointer without checking for null pointer. The pointer
may be null if the input is not complete and a line starts with a comma.
EricWF pushed a commit to efcs/llvm-project that referenced this pull request Oct 22, 2024
The part of the code for parsing Verilog module instantiations
dereferenced a pointer without checking for null pointer. The pointer
may be null if the input is not complete and a line starts with a comma.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants