Skip to contentSkip to conversation

ddr: socfpga: Fix integer overflow

PATCH In review Jit Loon Lim 2 participants 2 messages Aug 30, 2022 – Sep 2, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ddr: socfpga: Fix integer overflow

From: Tien Fong Chee <[email protected]>

dramaddrw data structure are defined as u32. Compiler assume 32-bit arithmetic on expression and the shifting operation to be done on a 32-bit value. So, the expression would overflow if the shifting is more than 32-bit.

---
 drivers/ddr/altera/sdram_soc64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ddr/altera/sdram_soc64.c +1 -1
index 9b1710c135..81ad02118b 100644
@@ -246,7 +246,7 @@ phys_size_t sdram_calculate_size(struct altera_sdram_plat *plat)
 			 DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw) +
 			 DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw));
 
-	size *= (2 << (hmc_ecc_readl(plat, DDRIOCTRL) &
+	size *= ((phys_size_t)2 << (hmc_ecc_readl(plat, DDRIOCTRL) &
 			DDR_HMC_DDRIOCTRL_IOSIZE_MSK));
 
 	return size;
Signature · 1 line
2.26.2
#1 lore raw
Quoted 17 lines · Jit Loon Lim

From: Tien Fong Chee <[email protected]>

dramaddrw data structure are defined as u32. Compiler assume 32-bit arithmetic on expression and the shifting operation to be done on a 32-bit value. So, the expression would overflow if the shifting is more than 32-bit.

---
  drivers/ddr/altera/sdram_soc64.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ddr/altera/sdram_soc64.c +0 -0
index 9b1710c135..81ad02118b 100644

drivers/ddr/altera/sdram_soc64.c phys_size_t sdram_calculate_size(struct altera_sdram_plat *plat) · line 249

Quoted 5 lines
drivers/ddr/altera/sdram_soc64.c +1 -1
@@ -246,7 +246,7 @@ phys_size_t sdram_calculate_size(struct altera_sdram_plat *plat)
  			 DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw) +
  			 DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw));
 
-	size *= (2 << (hmc_ecc_readl(plat, DDRIOCTRL) &
+	size *= ((phys_size_t)2 << (hmc_ecc_readl(plat, DDRIOCTRL) &

What about using 2UL or 2ULL here, would that work too ?