From ffad8a370aa29e69f4e8984e5daff76d22b06cce Mon Sep 17 00:00:00 2001 From: "Daniel R. Carvalho" Date: Thu, 26 Mar 2020 11:29:11 +0100 Subject: [PATCH] base: Fix undefined behavior in mask generation When generating a mask, if the number of bits is greater than the maximum shift distance (63), the shift will have undefined behavior. Previously the branch was taking care of a single trespassing case, and it has been fixed to cover the remaining. Issue-on: https://gem5.atlassian.net/browse/GEM5-205 Change-Id: Ib5a00917c8d2b23ffdb710c2f9673d956cd9f43e Signed-off-by: Daniel R. Carvalho Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/27104 Tested-by: Gem5 Cloud Project GCB service account <345032938727@cloudbuild.gserviceaccount.com> Tested-by: kokoro Reviewed-by: Gabe Black Maintainer: Gabe Black --- src/base/bitfield.hh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/base/bitfield.hh b/src/base/bitfield.hh index 9a252a406..c2ed72be2 100644 --- a/src/base/bitfield.hh +++ b/src/base/bitfield.hh @@ -50,12 +50,15 @@ extern const uint8_t reverseLookUpTable[]; /** - * Generate a 64-bit mask of 'nbits' 1s, right justified. + * Generate a 64-bit mask of 'nbits' 1s, right justified. If a number of bits + * greater than 64 is given, it is truncated to 64. + * + * @param nbits The number of bits set in the mask. */ inline uint64_t mask(int nbits) { - return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1; + return (nbits >= 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1; } /** -- 2.30.2