Skip to contentSkip to conversation

sd/pl181.c: Avoid undefined shift behaviour in RWORD macro

PATCH New Peter Maydell 1 participant 1 message Jun 27, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sd/pl181.c: Avoid undefined shift behaviour in RWORD macro

Add a cast to avoid potentially shifting into the sign bit of a signed value, which is undefined behaviour in C.

(Detected with clang's -fsanitize=undefined.)

---
Not the only problem clang detects by a long shot. I don't know if we even want to try to fix the warnings about loads of 32 bit values from misaligned addresses, but there are some more shift related warnings that we might as well zap.

 hw/sd/pl181.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/pl181.c +1 -1
index e08fd04..e5128c4 100644
@@ -175,7 +175,7 @@ static void pl181_send_command(pl181_state *s)
     if (rlen < 0)
         goto error;
     if (s->cmd & PL181_CMD_RESPONSE) {
-#define RWORD(n) ((response[n] << 24) | (response[n + 1] << 16) \
+#define RWORD(n) (((uint32_t)response[n] << 24) | (response[n + 1] << 16) \
                   | (response[n + 2] << 8) | response[n + 3])
         if (rlen == 0 || (rlen == 4 && (s->cmd & PL181_CMD_LONGRESP)))
             goto error;
Signature · 1 line
1.7.9.5