Skip to contentSkip to conversation

nvmem: core: Fix unintentional sign extension issue

PATCH In review Colin King 3 participants 4 messages Mar 11, 2021

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 nvmem: core: Fix unintentional sign extension issue

From: Colin Ian King <[email protected]>

The shifting of the u8 integer buf[3] by 24 bits to the left will be promoted to a 32 bit signed int and then sign-extended to a u64. In the event that the top bit of buf[3] is set then all then all the upper 32 bits of the u64 end up as also being set because of the sign-extension. Fix this by casting buf[i] to a u64 before the shift.

Addresses-Coverity: ("Unintended sign extension")

Fixes 097eb1136ebb ("nvmem: core: Add functions to make number reading easy")
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvmem/core.c +1 -1
index 635e3131eb5f..bca671ff4e54 100644
@@ -1693,7 +1693,7 @@ int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
 	/* Copy w/ implicit endian conversion */
 	*val = 0;
 	for (i = 0; i < len; i++)
-		*val |= buf[i] << (8 * i);
+		*val |= (uint64_t)buf[i] << (8 * i);
 
 	kfree(buf);
 
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

The shifting of the u8 integer buf[3] by 24 bits to the left will be promoted to a 32 bit signed int and then sign-extended to a u64. In the event that the top bit of buf[3] is set then all then all the upper 32 bits of the u64 end up as also being set because of the sign-extension. Fix this by casting buf[i] to a u64 before the shift.

Quoted 6 lines

Addresses-Coverity: ("Unintended sign extension")

Fixes 097eb1136ebb ("nvmem: core: Add functions to make number reading easy")
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks! I had only tested the "u64" version to read smaller data and store it in a u64. From my understanding of C rules, without your patch it would have been even worse than just a sign extension though, right? Shifting "buf[i]" by more than 32 bits would just not have worked right.

In any case:

#2 lore raw
Quoted 20 lines · Doug Anderson

Hi,

Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

The shifting of the u8 integer buf[3] by 24 bits to the left will be promoted to a 32 bit signed int and then sign-extended to a u64. In the event that the top bit of buf[3] is set then all then all the upper 32 bits of the u64 end up as also being set because of the sign-extension. Fix this by casting buf[i] to a u64 before the shift.

Quoted 6 lines

Addresses-Coverity: ("Unintended sign extension")

Fixes 097eb1136ebb ("nvmem: core: Add functions to make number reading easy")
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 5 lines

Thanks! I had only tested the "u64" version to read smaller data and store it in a u64. From my understanding of C rules, without your patch it would have been even worse than just a sign extension though, right? Shifting "buf[i]" by more than 32 bits would just not have worked right.

yep, that's correct, I forgot to mention that issue too :-/

Quoted 5 lines

In any case:

#3 lore raw
In reply to Colin King
Quoted 9 lines · Colin King

From: Colin Ian King <[email protected]>

The shifting of the u8 integer buf[3] by 24 bits to the left will be promoted to a 32 bit signed int and then sign-extended to a u64. In the event that the top bit of buf[3] is set then all then all the upper 32 bits of the u64 end up as also being set because of the sign-extension. Fix this by casting buf[i] to a u64 before the shift.

Quoted 4 lines

Addresses-Coverity: ("Unintended sign extension")

Fixes 097eb1136ebb ("nvmem: core: Add functions to make number reading easy")

---

Applied thanks,

--srini

Quoted 17 lines
  drivers/nvmem/core.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvmem/core.c +1 -1
index 635e3131eb5f..bca671ff4e54 100644
@@ -1693,7 +1693,7 @@ int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
  	/* Copy w/ implicit endian conversion */
  	*val = 0;
  	for (i = 0; i < len; i++)
-		*val |= buf[i] << (8 * i);
+		*val |= (uint64_t)buf[i] << (8 * i);
 
  	kfree(buf);