git: f1986e5b8863 - main - bhyve: Fix virtio-input event array resizing

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 19 Oct 2026 15:29:30 UTC
The branch main has been updated by markj:

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

commit f1986e5b88630b1404a202251499bca0f97f5f06
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-10-19 13:54:33 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-10-19 15:29:13 +0000

    bhyve: Fix virtio-input event array resizing
    
    Use the new array size when calling realloc().  Note that this queue
    stores events read from a host evdev device, i.e., it is not
    guest-controlled.
    
    While here, use the cdefs.h __unused macro and use them correctly, i.e.,
    don't mark "arg" as unused when it isn't.
    
    Reported by:    Brian Delgado from Intel using Anthropic Opus 4.8
    Fixes:          054accac71e0 ("Add a virtio-input device emulation.")
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D60679
---
 usr.sbin/bhyve/pci_virtio_input.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/usr.sbin/bhyve/pci_virtio_input.c b/usr.sbin/bhyve/pci_virtio_input.c
index 2da773df90be..29acdbfdfc16 100644
--- a/usr.sbin/bhyve/pci_virtio_input.c
+++ b/usr.sbin/bhyve/pci_virtio_input.c
@@ -467,19 +467,14 @@ static int
 vtinput_eventqueue_add_event(
     struct vtinput_eventqueue *queue, struct input_event *e)
 {
-	/* check if queue is full */
 	if (queue->idx >= queue->size) {
-		/* alloc new elements for queue */
-		const uint32_t newSize = queue->idx;
-		void *newPtr = realloc(queue->events,
-		    queue->size * sizeof(struct vtinput_event_elem));
-		if (newPtr == NULL) {
-			WPRINTF(("%s: realloc memory for eventqueue failed!",
-			    __func__));
+		const size_t nsize = queue->idx + VTINPUT_MAX_PKT_LEN;
+		void *ptr = realloc(queue->events,
+		    nsize * sizeof(struct vtinput_event_elem));
+		if (ptr == NULL)
 			return (1);
-		}
-		queue->events = newPtr;
-		queue->size = newSize;
+		queue->events = ptr;
+		queue->size = nsize;
 	}
 
 	/* save event */
@@ -584,8 +579,7 @@ vtinput_read_event_from_host(int fd, struct input_event *event)
 }
 
 static void
-vtinput_read_event(int fd __attribute((unused)),
-    enum ev_type t __attribute__((unused)), void *arg __attribute__((unused)))
+vtinput_read_event(int fd __unused, enum ev_type t __unused, void *arg)
 {
 	struct pci_vtinput_softc *sc = arg;