[PATCH] tools cpupower: Fix signed shift UB in amd_fam14h_idle

liujing posted 1 patch 3 weeks, 1 day ago
[PATCH] tools cpupower: Fix signed shift UB in amd_fam14h_idle
Posted by liujing 3 weeks, 1 day ago
From: Liu Jing <liujing@cmss.chinamobile.com>

In amd_fam14h_get_nb_power(), the expression `1 << 31` performs a
signed integer shift, which is undefined behavior in C.

Fix it by using `1U << 31` to perform an unsigned shift.

Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com>
---
--- a/tools/power/cpupower/utils/idle_monitor/amd_fam14h_idle.c
+++ b/tools/power/cpupower/utils/idle_monitor/amd_fam14h_idle.c
@@ -270,7 +270,7 @@
 {
 	uint32_t val;
 	val = pci_read_long(amd_fam14h_pci_dev, PCI_NBP1_CAP_OFFSET);
-	return val & (1 << 31);
+	return val & (1U << 31);
 }
 
 struct cpuidle_monitor *amd_fam14h_register(void)
Re: [PATCH] tools cpupower: Fix signed shift UB in amd_fam14h_idle
Posted by Shuah Khan 3 weeks ago
On 9/3/26 02:26, liujing wrote:
> From: Liu Jing <liujing@cmss.chinamobile.com>
> 
> In amd_fam14h_get_nb_power(), the expression `1 << 31` performs a
> signed integer shift, which is undefined behavior in C.
> 
> Fix it by using `1U << 31` to perform an unsigned shift.

How did you find this problem?

> 
> Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com>
> ---
> --- a/tools/power/cpupower/utils/idle_monitor/amd_fam14h_idle.c
> +++ b/tools/power/cpupower/utils/idle_monitor/amd_fam14h_idle.c
> @@ -270,7 +270,7 @@
>   {
>   	uint32_t val;
>   	val = pci_read_long(amd_fam14h_pci_dev, PCI_NBP1_CAP_OFFSET);
> -	return val & (1 << 31);
> +	return val & (1U << 31);
>   }
>   
>   struct cpuidle_monitor *amd_fam14h_register(void)
> 
> 
> 
> 

thanks,
-- Shuah