From caf6a507cb9eecb08673a9ab087af4d9b1ca32d9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isaac=20S=C3=A1nchez=20Barrera?= Date: Mon, 16 Nov 2020 16:22:44 +0100 Subject: [PATCH] base: Prevent undefined behavior in not interleaved `AddrRange`s. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit If an `AddrRange` is not interleaved, return the input address in `removeIntlvBits` and `addIntlvBits` to prevent undefined behavior. It allows to use these methods in all cases without having to check manually whether the range is interleaved. Change-Id: Ic6ac8c4e52b09417bc41aa9380a24319c34e0b35 Signed-off-by: Isaac Sánchez Barrera Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/37617 Maintainer: Bobby R. Bruce Tested-by: kokoro Reviewed-by: Nikos Nikoleris --- src/base/addr_range.hh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/base/addr_range.hh b/src/base/addr_range.hh index 8a811d495..7572d9790 100644 --- a/src/base/addr_range.hh +++ b/src/base/addr_range.hh @@ -473,12 +473,18 @@ class AddrRange * --------------------------------- * * @param a the input address - * @return the new address + * @return the new address, or the input address if not interleaved * * @ingroup api_addr_range */ inline Addr removeIntlvBits(Addr a) const { + // Directly return the address if the range is not interleaved + // to prevent undefined behavior. + if (!interleaved()) { + return a; + } + // Get the LSB set from each mask int masks_lsb[masks.size()]; for (int i = 0; i < masks.size(); i++) { @@ -511,6 +517,12 @@ class AddrRange */ inline Addr addIntlvBits(Addr a) const { + // Directly return the address if the range is not interleaved + // to prevent undefined behavior. + if (!interleaved()) { + return a; + } + // Get the LSB set from each mask int masks_lsb[masks.size()]; for (int i = 0; i < masks.size(); i++) { -- 2.30.2