From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: rafael@kernel.org, daniel.lezcano@linaro.org
Cc: rui.zhang@intel.com, lukasz.luba@arm.com,
jacob.jun.pan@linux.intel.com, jserv@ccns.ncku.edu.tw,
linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
Kuan-Wei Chiu <visitorckw@gmail.com>
Subject: [PATCH] tmon: Fix undefined behavior in left shift
Date: Mon, 1 Sep 2025 22:47:56 +0800 [thread overview]
Message-ID: <20250901144756.1179834-1-visitorckw@gmail.com> (raw)
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
next reply other threads:[~2025-09-01 14:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-01 14:47 Kuan-Wei Chiu [this message]
2025-09-01 15:00 ` Kuan-Wei Chiu
2025-10-29 6:57 ` Kuan-Wei Chiu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250901144756.1179834-1-visitorckw@gmail.com \
--to=visitorckw@gmail.com \
--cc=daniel.lezcano@linaro.org \
--cc=jacob.jun.pan@linux.intel.com \
--cc=jserv@ccns.ncku.edu.tw \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=lukasz.luba@arm.com \
--cc=rafael@kernel.org \
--cc=rui.zhang@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®