git: 411ea77fed69 - stable/13 - bhyve: Avoid triggering false -Wfree-nonheap-object warnings.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 26 Jan 2023 22:35:06 UTC
The branch stable/13 has been updated by jhb:

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

commit 411ea77fed696c6e93696b13d633a49317f05113
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-01-19 18:21:50 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-01-26 22:18:44 +0000

    bhyve: Avoid triggering false -Wfree-nonheap-object warnings.
    
    XHCI port and slot numbers are 1-based rather than 0-based.  To handle
    this, bhyve was subtracting one item from the pointers saved in the
    softc so that index 1 accessed index 0 of the allocated array.
    
    However, this is UB and confused GCC 12.  The compiler noticed that
    the calls to free() were using an offset and emitted a warning.
    Rather than storing UB pointers in the softc, push the decrement
    operation into the existing macros that wrap accesses to the relevant
    arrays.
    
    Reviewed by:    corvink, markj
    Differential Revision:  https://reviews.freebsd.org/D36829
    
    (cherry picked from commit b36b14beda4ff7ecbb906ada756141f76fcb81aa)
---
 usr.sbin/bhyve/pci_xhci.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/usr.sbin/bhyve/pci_xhci.c b/usr.sbin/bhyve/pci_xhci.c
index 5bc9498a4eb4..1eceb6fe0fd3 100644
--- a/usr.sbin/bhyve/pci_xhci.c
+++ b/usr.sbin/bhyve/pci_xhci.c
@@ -290,10 +290,10 @@ struct pci_xhci_softc {
 };
 
 
-/* portregs and devices arrays are set up to start from idx=1 */
-#define	XHCI_PORTREG_PTR(x,n)	&(x)->portregs[(n)]
-#define	XHCI_DEVINST_PTR(x,n)	(x)->devices[(n)]
-#define	XHCI_SLOTDEV_PTR(x,n)	(x)->slots[(n)]
+/* port and slot numbering start from 1 */
+#define	XHCI_PORTREG_PTR(x,n)	&((x)->portregs[(n) - 1])
+#define	XHCI_DEVINST_PTR(x,n)	((x)->devices[(n) - 1])
+#define	XHCI_SLOTDEV_PTR(x,n)	((x)->slots[(n) - 1])
 
 #define	XHCI_HALTED(sc)		((sc)->opregs.usbsts & XHCI_STS_HCH)
 
@@ -2738,10 +2738,6 @@ pci_xhci_parse_devices(struct pci_xhci_softc *sc, nvlist_t *nvl)
 	sc->devices = calloc(XHCI_MAX_DEVS, sizeof(struct pci_xhci_dev_emu *));
 	sc->slots = calloc(XHCI_MAX_SLOTS, sizeof(struct pci_xhci_dev_emu *));
 
-	/* port and slot numbering start from 1 */
-	sc->devices--;
-	sc->slots--;
-
 	ndevices = 0;
 
 	slots_nvl = find_relative_config_node(nvl, "slot");
@@ -2835,7 +2831,6 @@ pci_xhci_parse_devices(struct pci_xhci_softc *sc, nvlist_t *nvl)
 
 portsfinal:
 	sc->portregs = calloc(XHCI_MAX_DEVS, sizeof(struct pci_xhci_portregs));
-	sc->portregs--;
 
 	if (ndevices > 0) {
 		for (i = 1; i <= XHCI_MAX_DEVS; i++) {
@@ -2851,8 +2846,8 @@ bad:
 		free(XHCI_DEVINST_PTR(sc, i));
 	}
 
-	free(sc->devices + 1);
-	free(sc->slots + 1);
+	free(sc->devices);
+	free(sc->slots);
 
 	return (-1);
 }