diff options
author | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2024-10-01 17:42:36 +0200 |
---|---|---|
committer | Benjamin Tissoires <bentiss@kernel.org> | 2024-10-04 10:36:39 +0200 |
commit | 61595012f28036a58293df5a2ab75f80ca15c327 (patch) | |
tree | a119b068e20e1918a7026df18dc61aa8a749aa38 /drivers/hid | |
parent | Merge tag 'hid-for-linus-2024090201' of git://git.kernel.org/pub/scm/linux/ke... (diff) | |
download | linux-61595012f28036a58293df5a2ab75f80ca15c327.tar.xz linux-61595012f28036a58293df5a2ab75f80ca15c327.zip |
HID: simplify code in fetch_item()
We can easily calculate the size of the item using arithmetic (shifts).
This allows to pull duplicated code out of the switch statement, making
it cleaner.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Link: https://patch.msgid.link/ZvwYbESMZ667QZqY@google.com
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
Diffstat (limited to 'drivers/hid')
-rw-r--r-- | drivers/hid/hid-core.c | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 30de92d0bf0f..0dc3018de2a6 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -754,35 +754,32 @@ static const u8 *fetch_item(const __u8 *start, const __u8 *end, struct hid_item } item->format = HID_ITEM_FORMAT_SHORT; - item->size = b & 3; + item->size = BIT(b & 3) >> 1; /* 0, 1, 2, 3 -> 0, 1, 2, 4 */ + + if (end - start < item->size) + return NULL; switch (item->size) { case 0: - return start; + break; case 1: - if ((end - start) < 1) - return NULL; - item->data.u8 = *start++; - return start; + item->data.u8 = *start; + break; case 2: - if ((end - start) < 2) - return NULL; item->data.u16 = get_unaligned_le16(start); - start = (__u8 *)((__le16 *)start + 1); - return start; + break; - case 3: - item->size++; - if ((end - start) < 4) - return NULL; + case 4: item->data.u32 = get_unaligned_le32(start); - start = (__u8 *)((__le32 *)start + 1); - return start; + break; + + default: + unreachable(); } - return NULL; + return start + item->size; } static void hid_scan_input_usage(struct hid_parser *parser, u32 usage) |