From 94b44a37b236684ccfbee970ae494b425c22fcca Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:18:49 +1200 Subject: [PATCH 1/2] rtlil.cc: Fix #4427 If a `RTLIL::Const` is composed of multiple strings, such as when using a ternary expression to select between two strings of different lengths, zero padding for the strings needs to be maintained. Only leading (and trailing) null characters should be dropped from the decoded string, rather than all null characters. --- kernel/rtlil.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index a6aebaa42..eb3fc938a 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -325,8 +325,7 @@ std::string RTLIL::Const::decode_string() const ch |= 1 << j; } } - if (ch != 0) - s.append({ch}); + s.append({ch}); } i -= 8; for (; i >= 0; i -= 8) { @@ -336,10 +335,9 @@ std::string RTLIL::Const::decode_string() const ch |= 1 << j; } } - if (ch != 0) - s.append({ch}); + s.append({ch}); } - return s; + return s.substr(s.find_first_not_of('\0')); } bool RTLIL::Const::is_fully_zero() const From 260cc42c2f635bafcff3e5aa574528759db52096 Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:32:42 +1200 Subject: [PATCH 2/2] rtlil.cc: Fix decode for empty string --- kernel/rtlil.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index eb3fc938a..50e28bba7 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -337,7 +337,10 @@ std::string RTLIL::Const::decode_string() const } s.append({ch}); } - return s.substr(s.find_first_not_of('\0')); + auto first_char = s.find_first_not_of('\0'); + if (first_char != std::string::npos) + return s.substr(first_char); + else return s; } bool RTLIL::Const::is_fully_zero() const