git: 3ab6dae4b469 - main - TMP461 sensor: Fix UB when reading the temperature

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 11:08:06 UTC
The branch main has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=3ab6dae4b469279b7ba5c90b27ba3597e2e8105c

commit 3ab6dae4b469279b7ba5c90b27ba3597e2e8105c
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2026-11-17 18:07:29 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2026-11-25 11:07:36 +0000

    TMP461 sensor: Fix UB when reading the temperature
    
    Left shifting a potentially negative number is undefined behavior
    according to the C standard (still in C23, although that's probably
    a mistake for this one).
    
    Shift left the just-read data first, as it is of an unsigned type
    (uint8_t), and then sign-extend the result with the new sign_extend32().
    
    Reviewed by:    manu
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D61763
---
 sys/dev/iicbus/sensor/tmp461.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/sys/dev/iicbus/sensor/tmp461.c b/sys/dev/iicbus/sensor/tmp461.c
index d30230ee7da3..69849742cbc1 100644
--- a/sys/dev/iicbus/sensor/tmp461.c
+++ b/sys/dev/iicbus/sensor/tmp461.c
@@ -263,7 +263,11 @@ tmp461_read_temperature(device_t dev, int32_t *temperature, bool remote_measure)
 		goto fail;
 
 	data -= offset;
-	*temperature = extract_signed_bitfield_32(data, 0, 8) << 4;
+	/*
+	 * Sign extension has to happen at the end, as left shift of negative
+	 * signed integer has undefined behavior.
+	 */
+	*temperature = sign_extend_32(data << 4, 11);
 
 	if (remote_measure) {
 		if (sc->conf & TMP461_REMOTE_TEMP_DOUBLE_REG) {