linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] HID: simplify code in fetch_item()
@ 2024-10-01 15:42 Dmitry Torokhov
  2024-10-04 12:05 ` Benjamin Tissoires
  2024-10-10 22:24 ` Nathan Chancellor
  0 siblings, 2 replies; 12+ messages in thread
From: Dmitry Torokhov @ 2024-10-01 15:42 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires; +Cc: linux-input, linux-kernel

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>
---
 drivers/hid/hid-core.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 988d0acbdf04..00942d40fe08 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -754,35 +754,32 @@ static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *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)
-- 
2.46.1.824.gd892dcdcdd-goog


-- 
Dmitry

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2025-04-16  6:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-01 15:42 [PATCH] HID: simplify code in fetch_item() Dmitry Torokhov
2024-10-04 12:05 ` Benjamin Tissoires
2024-10-10 22:24 ` Nathan Chancellor
2024-10-15 18:28   ` Dmitry Torokhov
2024-10-15 18:56     ` Paul E. McKenney
2024-10-15 19:26     ` Nathan Chancellor
2024-10-15 20:59       ` Segher Boessenkool
2025-04-14  6:30   ` Andy Shevchenko
2025-04-15  0:33     ` Nathan Chancellor
2025-04-15  6:45       ` Andy Shevchenko
2025-04-15 15:21         ` Nathan Chancellor
2025-04-16  6:48           ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).