Wed Aug 28 06:07:21 2019 UTC ()
in usbnet_detach(), check both that the private pointer has
been allocated and that unp_attached is true before trying
to access anything else.

fixes a detach problem noticed by maxv when, eg, the call
to usbd_set_config_no() fails and attach bails early.


(mrg)
diff -r1.23 -r1.24 src/sys/dev/usb/usbnet.c

cvs diff -r1.23 -r1.24 src/sys/dev/usb/usbnet.c (expand / switch to context diff)
--- src/sys/dev/usb/usbnet.c 2019/08/23 04:29:28 1.23
+++ src/sys/dev/usb/usbnet.c 2019/08/28 06:07:21 1.24
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbnet.c,v 1.23 2019/08/23 04:29:28 mrg Exp $	*/
+/*	$NetBSD: usbnet.c,v 1.24 2019/08/28 06:07:21 mrg Exp $	*/
 
 /*
  * Copyright (c) 2019 Matthew R. Green
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.23 2019/08/23 04:29:28 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.24 2019/08/28 06:07:21 mrg Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -1457,17 +1457,18 @@
 {
 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
 	struct usbnet * const un = device_private(self);
+	struct usbnet_private * const unp = un->un_pri;
+
+	/* Detached before attached finished, so just bail out. */
+	if (unp == NULL || !unp->unp_attached)
+		return 0;
+
 	struct ifnet * const ifp = usbnet_ifp(un);
 	struct mii_data * const mii = usbnet_mii(un);
-	struct usbnet_private * const unp = un->un_pri;
 
 	mutex_enter(&unp->unp_lock);
 	unp->unp_dying = true;
 	mutex_exit(&unp->unp_lock);
-
-	/* Detached before attached finished, so just bail out. */
-	if (!unp->unp_attached)
-		return 0;
 
 	callout_halt(&unp->unp_stat_ch, NULL);
 	usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, NULL);