Skip to content

formatter: avoid crash on attribute instance in for-loop header - #2585

Open
EylonKrause wants to merge 1 commit into
chipsalliance:masterfrom
EylonKrause:fix-forspec-attribute-crash
Open

formatter: avoid crash on attribute instance in for-loop header#2585
EylonKrause wants to merge 1 commit into
chipsalliance:masterfrom
EylonKrause:fix-forspec-attribute-crash

Conversation

@EylonKrause

Copy link
Copy Markdown
Contributor

Problem

verible-verilog-format aborts with a CHECK failure (SIGABRT, exit 134)
when an attribute instance (* ... *) appears in a for-loop header.
TreeUnwrapper::ReshapeTokenPartitions' kForSpec case assumes the header
always produces two semicolon-leading partitions:

CHECK(iter1 != children.end());
...
CHECK(iter2 != children.end());

When an attribute instance appears in the init/condition, the partition
structure differs and a semicolon-leading partition is missing, firing the
CHECK and aborting the process on otherwise valid input.

Minimal repro

module m; initial for(int i=0(* a *);;) ; endmodule
module m; initial for(int i=0(* a *);i<4;i++) x=i; endmodule

Before this change both abort:

F tree-unwrapper.cc:2996] Check failed: iter2 != children.end()
*** Check failure stack trace: ***
... SIGABRT (exit 134)

Fix

Replace the two unconditional CHECKs with graceful early-outs: when a
required semicolon-leading partition is not found, leave the partitions
unreshaped instead of aborting. Normal for-loops (both partitions present)
reshape exactly as before.

After the change the same inputs format without aborting, e.g.:

module m;
  initial for (int i = 0 (* a *); i < 4; i++) x = i;
endmodule

Testing

  • bazel test -c opt //verible/verilog/formatting:tree-unwrapper_test //verible/verilog/formatting:formatter_test — both pass.
  • Added a formatter regression test for the attribute-in-for-header input.
  • Verified a normal for-loop (for (int i=0; i<4; i++)) formats unchanged.

Disclosure: this contribution was authored with an AI coding assistant (Claude) and reviewed before submission.

TreeUnwrapper::ReshapeTokenPartitions' kForSpec case assumed the
for-loop header always yields two semicolon-leading partitions and
CHECK-failed (SIGABRT) otherwise. An attribute instance ((* ... *)) in
the for-loop init/condition changes the partition structure so those
partitions are absent, aborting the formatter on otherwise valid input,
e.g.:

  module m; initial for(int i=0(* a *);i<4;i++) x=i; endmodule

Replace the two CHECKs with graceful early-outs: when a required
semicolon-leading partition is not found, leave the partitions
unreshaped instead of aborting. Normal for-loops (both partitions
present) reshape exactly as before. Adds a formatter regression test.

Signed-off-by: Eylon Krause <eylon1909@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant