From 2b9982101aa01213f880afcebd5d7ea2ce520d95 Mon Sep 17 00:00:00 2001 From: Aki Van Ness Date: Wed, 24 Aug 2022 11:31:28 -0400 Subject: [PATCH] kernel: rtlil: imposed a hard limit on RTLIL::Const of 2^24 (closes #3317) --- kernel/rtlil.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 5211c3b3f..8df719aa9 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -201,6 +201,9 @@ const pool &RTLIL::builtin_ff_cell_types() { RTLIL::Const::Const(const std::string &str) { + if (str.size() * 8 > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_STRING; bits.reserve(str.size() * 8); for (int i = str.size()-1; i >= 0; i--) { @@ -214,6 +217,12 @@ RTLIL::Const::Const(const std::string &str) RTLIL::Const::Const(int val, int width) { + if (width < 0) + throw std::length_error("RTLIL Const width must not be negative"); + + if (width > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_NONE; bits.reserve(width); for (int i = 0; i < width; i++) { @@ -224,6 +233,12 @@ RTLIL::Const::Const(int val, int width) RTLIL::Const::Const(RTLIL::State bit, int width) { + if (width < 0) + throw std::length_error("RTLIL Const width must not be negative"); + + if (width > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_NONE; bits.reserve(width); for (int i = 0; i < width; i++) @@ -232,6 +247,10 @@ RTLIL::Const::Const(RTLIL::State bit, int width) RTLIL::Const::Const(const std::vector &bits) { + + if (bits.size() > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_NONE; this->bits.reserve(bits.size()); for (const auto &b : bits) @@ -296,6 +315,10 @@ std::string RTLIL::Const::as_string() const RTLIL::Const RTLIL::Const::from_string(const std::string &str) { + + if (str.size() > 0x1000000) + throw std::length_error("RTLIL width must be less than 2^24"); + Const c; c.bits.reserve(str.size()); for (auto it = str.rbegin(); it != str.rend(); it++)