git: a793cabb621e - main - libusb: Avoid signed integer overflow UB

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Fri, 21 Aug 2026 19:50:16 UTC
The branch main has been updated by emaste:

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

commit a793cabb621ee33ded7ef25cf8290ee53d2c5e4d
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2026-08-21 16:56:08 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2026-08-21 19:49:44 +0000

    libusb: Avoid signed integer overflow UB
    
    Instead, just reset next_callback_id to 1 at INT_MAX.  The potential for
    duplicate callback IDs remains.
    
    Sponsored by:   The FreeBSD Foundation
---
 lib/libusb/libusb10_hotplug.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/libusb/libusb10_hotplug.c b/lib/libusb/libusb10_hotplug.c
index ae7e8d168948..5def972c5ab5 100644
--- a/lib/libusb/libusb10_hotplug.c
+++ b/lib/libusb/libusb10_hotplug.c
@@ -374,10 +374,15 @@ int libusb_hotplug_register_callback(libusb_context *ctx,
 	handle->devclass = dev_class;
 	handle->fn = cb_fn;
 	handle->user_data = user_data;
+
 	CTX_LOCK(ctx);
-	/* XXX This could result in duplicate callback IDs, and is UB. */
-	if ((handle->id = ctx->next_callback_id++) < 0)
-		handle->id = ctx->next_callback_id = 1;
+	handle->id = ctx->next_callback_id;
+	if (ctx->next_callback_id == INT_MAX) {
+		/* XXX This could result in duplicate callback IDs. */
+		ctx->next_callback_id = 1;
+	} else {
+		ctx->next_callback_id++;
+	}
 	CTX_UNLOCK(ctx);
 
 	HOTPLUG_LOCK(ctx);