tmon: Fix undefined behavior in left shift
PATCH 1/1 tmon: Fix undefined behavior in left shift
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.
--- tools/thermal/tmon/tmon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/thermal/tmon/tmon.c +1 -1
@@ -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",
Signature · 1 line
2.34.1
Quoted 6 lines · 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'
I forgot to mention in the commit message that the UBSAN report was triggered when running sudo ./tmon -l. Let me know if you'd like me to include that detail, and I can update and resend as v2.
Regards,
Kuan-Wei
Quoted 8 lines · 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:
Quoted 8 lines
"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 <[email protected]>
I know maintainers are busy people, so I usually try to avoid sending ping messages. However, this patch was submitted about two months ago and hasn't received any feedback yet. It looks like it might have somehow got lost?
Regards,
Kuan-Wei