public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tmon: Fix undefined behavior in left shift
@ 2025-09-01 14:47 Kuan-Wei Chiu
  2025-09-01 15:00 ` Kuan-Wei Chiu
  2025-10-29  6:57 ` Kuan-Wei Chiu
  0 siblings, 2 replies; 3+ messages in thread
From: Kuan-Wei Chiu @ 2025-09-01 14:47 UTC (permalink / raw)
  To: rafael, daniel.lezcano
  Cc: rui.zhang, lukasz.luba, jacob.jun.pan, jserv, linux-pm,
	linux-kernel, Kuan-Wei Chiu

Using 1 << j when j reaches 31 triggers undefined behavior because
the constant 1 is of type int, and shifting it left by 31 exceeds
the range of signed int. UBSAN reports:

tmon.c:174:54: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'

According to the C11 standard:

"If E1 has a signed type and E1 x 2^E2 is not representable in the
result type, the behavior is undefined."

Fix this by using 1U << j, ensuring the shift is performed on an
unsigned type where all 32 bits are representable.

Fixes: 94f69966faf8 ("tools/thermal: Introduce tmon, a tool for thermal subsystem")
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 tools/thermal/tmon/tmon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/thermal/tmon/tmon.c b/tools/thermal/tmon/tmon.c
index 7eb3216a27f4..ef67cd1a4861 100644
--- a/tools/thermal/tmon/tmon.c
+++ b/tools/thermal/tmon/tmon.c
@@ -171,7 +171,7 @@ static void prepare_logging(void)
 
 		memset(binding_str, 0, sizeof(binding_str));
 		for (j = 0; j < 32; j++)
-			binding_str[j] = (ptdata.tzi[i].cdev_binding & (1 << j)) ?
+			binding_str[j] = (ptdata.tzi[i].cdev_binding & (1U << j)) ?
 				'1' : '0';
 
 		fprintf(tmon_log, "#thermal zone %s%02d cdevs binding: %32s\n",
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-29  6:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-01 14:47 [PATCH] tmon: Fix undefined behavior in left shift Kuan-Wei Chiu
2025-09-01 15:00 ` Kuan-Wei Chiu
2025-10-29  6:57 ` Kuan-Wei Chiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox