Skip to content

Commit c8f87d6

Browse files
committed
Use int.bit_length() when checking fitness for assignment width in order to avoid OOM insanity #224
1 parent afaf420 commit c8f87d6

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

src/systemrdl/core/ExprVisitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def visitNumberVerilog(self, ctx: SystemRDLParser.NumberVerilogContext):
118118
src_ref_from_antlr(ctx.VLOG_INT())
119119
)
120120

121-
if val >= (1 << width):
121+
if val.bit_length() > width:
122122
self.msg.fatal(
123123
"Value of integer literal exceeds the specified width",
124124
src_ref_from_antlr(ctx.VLOG_INT())

src/systemrdl/properties/builtin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def validate(self, node: m_node.Node, value: Any) -> None:
7676
# 5.2.2.1-a: If value is a bit mask, the mask shall have the same width
7777
# as the field
7878
if not isinstance(value, bool) and isinstance(value, int):
79-
if value >= (1 << node.width):
79+
if value.bit_length() > node.width:
8080
self.env.msg.error(
8181
"Bit mask (%d) of property 'dontcompare' exceeds width (%d) of field '%s'"
8282
% (value, node.width, node.inst_name),
@@ -433,7 +433,7 @@ def validate(self, node: m_node.Node, value: Any) -> None:
433433
assert isinstance(node, m_node.FieldNode)
434434
if isinstance(value, int):
435435
# 9.5.1-c: The reset value cannot be larger than can fit in the field
436-
if value >= (1 << node.width):
436+
if value.bit_length() > node.width:
437437
self.env.msg.error(
438438
"The reset value (%d) of field '%s' cannot fit within its width (%d)"
439439
% (value, node.inst_name, node.width),

src/systemrdl/properties/user_defined.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def validate(self, node: m_node.Node, value: Any) -> None:
9090

9191
# Spec does not specify, but assuming this check gets ignored for
9292
# non-vector nodes
93-
if isinstance(node, m_node.VectorNode):
94-
if value >= (1 << node.width):
93+
if isinstance(node, m_node.VectorNode) and isinstance(value, int):
94+
if value.bit_length() > node.width:
9595
self.env.msg.error(
9696
"Value (%d) of the '%s' property cannot fit within the width (%d) of component '%s'"
9797
% (value, self.name, node.width, node.inst_name),

0 commit comments

Comments
 (0)