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

[bug][slang] Fix case selector huge bitwidth segmentation fault #1217

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions source/ast/statements/ConditionalStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,22 @@ class CaseTrie {
if (auto result = find(value, 0, expr, wildcardX))
return result;

CaseTrie* curr = this;
Node curr;
curr.trie = this;
const auto width = value.getBitWidth();
for (uint32_t i = 0; i < width; i++) {
const auto bit = value[(int32_t)i];
const bool isWildcard = wildcardX ? bit.isUnknown() : bit.value == logic_t::Z_VALUE;
const auto valueIndex = isWildcard ? 3 : bit.isUnknown() ? 2 : bit.value;

auto& elem = curr->nodes[valueIndex];
auto& elem = curr.trie->nodes[valueIndex];
if (i == width - 1) {
elem.expr = &expr;
}
else {
if (!elem.trie)
elem.trie = alloc.emplace<CaseTrie>();
curr = elem.trie;
curr = elem;
}
}

Expand Down Expand Up @@ -284,9 +285,9 @@ class CaseTrie {
return nullptr;
}

union Node {
CaseTrie* trie;
const Expression* expr;
struct Node {
CaseTrie* trie = nullptr;
const Expression* expr = nullptr;
};
Node nodes[4] = {};
};
Expand Down
24 changes: 24 additions & 0 deletions tests/unittests/ast/WarningTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,30 @@ endmodule
CHECK(diags[6].code == diag::CaseOverlap);
}

TEST_CASE("Case statement with huge bit width selector") {
auto tree = SyntaxTree::fromText(R"(
module test9(
input reg [100:0] sel_i,
input reg trg_i,
output reg [1:0] reg_o
);
always @(posedge trg_i) begin
casex (sel_i)
-12'b00zzzzz00001,
12'b11: reg_o = 2'b01;
endcase
end
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);

auto& diags = compilation.getAllDiagnostics();
REQUIRE(diags.size() == 1);
CHECK(diags[0].code == diag::CaseDefault);
}

TEST_CASE("Case items with unknowns that are not wildcards") {
auto tree = SyntaxTree::fromText(R"(
module m;
Expand Down
Loading