Skip to content

Commit

Permalink
[armv8][mpu] Fix undefined behaviour in shifting 8 bit value (#1668)
Browse files Browse the repository at this point in the history
  • Loading branch information
bentank authored Jul 31, 2024
1 parent 9aa50b3 commit f979a19
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CMSIS/Core/Include/mpu_armv8.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f979a19

Please sign in to comment.