hw/i2c/microbit_i2c: Don't index off end of twi_read_sequence[]
If the guest tries to read more bytes from our fake stub I2C device than we have provided, we incorrectly read one byte beyond the end of this array. Avoid this, and instead keep reporting the RXD register as containing the last byte of the "data transfer". Cc: qemu-stable@nongnu.org Fixes: 9d68bf564ec ("arm: Stub out NRF51 TWI magnetometer/accelerometer detection") Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3408 Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-ID: <20260501162634.4092394-1-peter.maydell@linaro.org> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Peter Maydell committed
May 1, 2026 at 17:26 UTC
a824f3531a44cbd19bcd9dd0ca48e5805c781e02
1 file changed
+6
-1
hw/i2c/microbit_i2c.c
+6
-1
@@ -41,8 +41,13 @@ static uint64_t microbit_i2c_read(void *opaque, hwaddr addr, unsigned int size)
41
data = 0x01;
42
break;
43
case NRF51_TWI_REG_RXD:
44
+ /*
45
+ * Return the next byte from our fake data sequence. If
46
+ * the guest keeps reading the register after that, keep
47
+ * returning the same last byte value.
48
+ */
49
data = twi_read_sequence[s->read_idx];
45
- if (s->read_idx < G_N_ELEMENTS(twi_read_sequence)) {
50
+ if (s->read_idx + 1 < G_N_ELEMENTS(twi_read_sequence)) {
51
s->read_idx++;
52
}
53
break;