From 4c728968a3cedb90dae206e34624d831997d1ecf Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Fri, 14 Feb 2025 13:29:24 +1300 Subject: [PATCH] Fix runtime error on shr INT_MAX --- passes/opt/opt_expr.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/passes/opt/opt_expr.cc b/passes/opt/opt_expr.cc index ac4a65156c9..62a0ffc4814 100644 --- a/passes/opt/opt_expr.cc +++ b/passes/opt/opt_expr.cc @@ -1315,6 +1315,10 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID::A)); RTLIL::SigSpec sig_y(cell->type == ID($shiftx) ? RTLIL::State::Sx : RTLIL::State::S0, cell->getParam(ID::Y_WIDTH).as_int()); + // Limit indexing to the size of a, which is behaviourally identical (result is all 0) + // and avoids integer overflow of i + shift_bits when e.g. ID::B == INT_MAX + shift_bits = min(shift_bits, GetSize(sig_a)); + if (cell->type != ID($shiftx) && GetSize(sig_a) < GetSize(sig_y)) sig_a.extend_u0(GetSize(sig_y), cell->getParam(ID::A_SIGNED).as_bool());