From f979a19f8e923d85946cab56fb815a11a9e9a5a7 Mon Sep 17 00:00:00 2001 From: bentank Date: Tue, 30 Jul 2024 23:06:20 -0700 Subject: [PATCH] [armv8][mpu] Fix undefined behaviour in shifting 8 bit value (#1668) --- CMSIS/Core/Include/mpu_armv8.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMSIS/Core/Include/mpu_armv8.h b/CMSIS/Core/Include/mpu_armv8.h index 3a0aa575a5..03a04330fd 100644 --- a/CMSIS/Core/Include/mpu_armv8.h +++ b/CMSIS/Core/Include/mpu_armv8.h @@ -261,12 +261,13 @@ __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t at const uint8_t reg = idx / 4U; const uint32_t pos = ((idx % 4U) * 8U); const uint32_t mask = 0xFFU << pos; + const uint32_t val = (uint32_t)attr << pos; if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) { return; // invalid index } - mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | ((attr << pos) & mask)); + mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | (val & mask)); } /** Set the memory attribute encoding.