]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
HID: simplify snto32()
authorDmitry Torokhov <dmitry.torokhov@gmail.com>
Thu, 3 Oct 2024 14:46:50 +0000 (07:46 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 9 Sep 2025 16:58:04 +0000 (18:58 +0200)
[ Upstream commit ae9b956cb26c0fd5a365629f2d723ab2fb14df79 ]

snto32() does exactly what sign_extend32() does, but handles
potentially malformed data coming from the device. Keep the checks,
but then call sign_extend32() to perform the actual conversion.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Link: https://patch.msgid.link/20241003144656.3786064-1-dmitry.torokhov@gmail.com
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
Stable-dep-of: a6b87bfc2ab5 ("HID: core: Harden s32ton() against conversion to 0 bits")
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/hid/hid-core.c

index c2783d04c6e05019a95d01e45c842ced7b56edc7..1a8e88624acfb20bd71a72419baebd7ed6b3a3b6 100644 (file)
@@ -1318,9 +1318,7 @@ alloc_err:
 EXPORT_SYMBOL_GPL(hid_open_report);
 
 /*
- * Convert a signed n-bit integer to signed 32-bit integer. Common
- * cases are done through the compiler, the screwed things has to be
- * done by hand.
+ * Convert a signed n-bit integer to signed 32-bit integer.
  */
 
 static s32 snto32(__u32 value, unsigned n)
@@ -1331,12 +1329,7 @@ static s32 snto32(__u32 value, unsigned n)
        if (n > 32)
                n = 32;
 
-       switch (n) {
-       case 8:  return ((__s8)value);
-       case 16: return ((__s16)value);
-       case 32: return ((__s32)value);
-       }
-       return value & (1 << (n - 1)) ? value | (~0U << n) : value;
+       return sign_extend32(value, n - 1);
 }
 
 s32 hid_snto32(__u32 value, unsigned n)