Skip to contentSkip to conversation

hw/ide/ahci.c: Fix shift left into sign bit

PATCH In review Peter Maydell 2 participants 3 messages Oct 16, 2015 – Oct 18, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ide/ahci.c: Fix shift left into sign bit

Avoid undefined behaviour from shifting left into the sign bit:

hw/ide/ahci.c:551:36: runtime error: left shift of 255 by 24 places cannot be represented in type 'int'

(Unfortunately C's promotion rules mean that in the expression "some_uint8_t_variable << 24" the LHS gets promoted to signed int before shifting.)

---
clang's undefined sanitizer produces a lot of copies of this warning during 'make check'...

 hw/ide/ahci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/ahci.c +1 -1
index 796be15..21f76ed 100644
@@ -548,7 +548,7 @@ static void ahci_init_d2h(AHCIDevice *ad)
         ad->init_d2h_sent = true;
         /* We're emulating receiving the first Reg H2D Fis from the device;
          * Update the SIG register, but otherwise proceed as normal. */
-        pr->sig = (ide_state->hcyl << 24) |
+        pr->sig = ((uint32_t)ide_state->hcyl << 24) |
             (ide_state->lcyl << 16) |
             (ide_state->sector << 8) |
             (ide_state->nsector & 0xFF);
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Peter Maydell

Avoid undefined behaviour from shifting left into the sign bit:

hw/ide/ahci.c:551:36: runtime error: left shift of 255 by 24 places cannot be represented in type 'int'

(Unfortunately C's promotion rules mean that in the expression "some_uint8_t_variable << 24" the LHS gets promoted to signed int before shifting.)

---
clang's undefined sanitizer produces a lot of copies of this warning during 'make check'...

 hw/ide/ahci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/ahci.c +0 -0
index 796be15..21f76ed 100644

hw/ide/ahci.c static void ahci_init_d2h(AHCIDevice *ad) · line 554

Quoted 8 lines
hw/ide/ahci.c +1 -1
@@ -548,7 +548,7 @@ static void ahci_init_d2h(AHCIDevice *ad)
         ad->init_d2h_sent = true;
         /* We're emulating receiving the first Reg H2D Fis from the device;
          * Update the SIG register, but otherwise proceed as normal. */
-        pr->sig = (ide_state->hcyl << 24) |
+        pr->sig = ((uint32_t)ide_state->hcyl << 24) |
             (ide_state->lcyl << 16) |
             (ide_state->sector << 8) |
             (ide_state->nsector & 0xFF);

Since the "patches" tool seems to still be hiccuping, do you want to just apply this directly?

--js

#2 lore raw
Quoted 36 lines · John Snow
Quoted 22 lines · Peter Maydell

Avoid undefined behaviour from shifting left into the sign bit:

hw/ide/ahci.c:551:36: runtime error: left shift of 255 by 24 places cannot be represented in type 'int'

(Unfortunately C's promotion rules mean that in the expression "some_uint8_t_variable << 24" the LHS gets promoted to signed int before shifting.)

---
clang's undefined sanitizer produces a lot of copies of this warning during 'make check'...

 hw/ide/ahci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/ahci.c +0 -0
index 796be15..21f76ed 100644

hw/ide/ahci.c static void ahci_init_d2h(AHCIDevice *ad) · line 554

Quoted 8 lines
hw/ide/ahci.c +1 -1
@@ -548,7 +548,7 @@ static void ahci_init_d2h(AHCIDevice *ad)
         ad->init_d2h_sent = true;
         /* We're emulating receiving the first Reg H2D Fis from the device;
          * Update the SIG register, but otherwise proceed as normal. */
-        pr->sig = (ide_state->hcyl << 24) |
+        pr->sig = ((uint32_t)ide_state->hcyl << 24) |
             (ide_state->lcyl << 16) |
             (ide_state->sector << 8) |
             (ide_state->nsector & 0xFF);
Quoted 2 lines

Since the "patches" tool seems to still be hiccuping, do you want to just apply this directly?

OK, applied to master.

thanks
-- PMM