Skip to content

Commit 6159501

Browse files
dtorBenjamin Tissoires
authored andcommitted
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 <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent f23aa4c commit 6159501

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

drivers/hid/hid-core.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -754,35 +754,32 @@ static const u8 *fetch_item(const __u8 *start, const __u8 *end, struct hid_item
754754
}
755755

756756
item->format = HID_ITEM_FORMAT_SHORT;
757-
item->size = b & 3;
757+
item->size = BIT(b & 3) >> 1; /* 0, 1, 2, 3 -> 0, 1, 2, 4 */
758+
759+
if (end - start < item->size)
760+
return NULL;
758761

759762
switch (item->size) {
760763
case 0:
761-
return start;
764+
break;
762765

763766
case 1:
764-
if ((end - start) < 1)
765-
return NULL;
766-
item->data.u8 = *start++;
767-
return start;
767+
item->data.u8 = *start;
768+
break;
768769

769770
case 2:
770-
if ((end - start) < 2)
771-
return NULL;
772771
item->data.u16 = get_unaligned_le16(start);
773-
start = (__u8 *)((__le16 *)start + 1);
774-
return start;
772+
break;
775773

776-
case 3:
777-
item->size++;
778-
if ((end - start) < 4)
779-
return NULL;
774+
case 4:
780775
item->data.u32 = get_unaligned_le32(start);
781-
start = (__u8 *)((__le32 *)start + 1);
782-
return start;
776+
break;
777+
778+
default:
779+
unreachable();
783780
}
784781

785-
return NULL;
782+
return start + item->size;
786783
}
787784

788785
static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)

0 commit comments

Comments
 (0)