From 33a22b5cd135b55b9e4d0f195832282be542e4b2 Mon Sep 17 00:00:00 2001 From: George Rennie Date: Mon, 26 May 2025 15:28:14 +0100 Subject: [PATCH] kernel: fix convertible_to_int for overflowing unsigned values --- kernel/rtlil.cc | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index c08e78dce..021aff73b 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -383,7 +383,23 @@ int RTLIL::Const::as_int(bool is_signed) const bool RTLIL::Const::convertible_to_int(bool is_signed) const { auto size = get_min_size(is_signed); - return (size > 0 && size <= 32); + + if (size <= 0) + return false; + + // If it fits in 31 bits it is definitely convertible + if (size <= 31) + return true; + + // If it fits in 32 bits, it is convertible if signed or if unsigned and the + // leading bit is not 1 + if (size == 32) { + if (is_signed) + return true; + return get_bits().at(31) != State::S1; + } + + return false; } std::optional RTLIL::Const::try_as_int(bool is_signed) const