From ddf65939f757d508cf47f0a4f8057d7eed9db060 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Tue, 30 Sep 2025 05:09:39 +0200
Subject: [PATCH] kernel: Don't call bcopy() and memcpy() when src == NULL.

Although it does seem to do no harm with the __builtin_mem{cpy,move}()
which it ultimately resolves to, the upcoming gcc 12.5's -Wnonnull
will whine about the several cases where it did happen for one reason
or another.

While here, change the 4th argument of the usbd_do_request() macro in
a few of these cases to NULL because it is a pointer.
---
 sys/bus/u4b/usb_request.c | 35 ++++++++++++++++++-----------------
 sys/kern/imgact_elf.c     |  2 +-
 2 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/sys/bus/u4b/usb_request.c b/sys/bus/u4b/usb_request.c
index 43037edaca..9b29ae795f 100644
--- a/sys/bus/u4b/usb_request.c
+++ b/sys/bus/u4b/usb_request.c
@@ -517,7 +517,8 @@ usbd_do_request_flags(struct usb_device *udev, struct lock *lock,
 				}
 			} else
 #endif
-				memcpy(data, desc, length);
+				if (data != NULL)
+					memcpy(data, desc, length);
 		}
 		goto done;		/* success */
 	}
@@ -1446,7 +1447,7 @@ usbd_req_set_alt_interface_no(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
 	req.wIndex[1] = 0;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1620,7 +1621,7 @@ usbd_req_clear_hub_feature(struct usb_device *udev, struct lock *lock,
 	USETW(req.wValue, sel);
 	USETW(req.wIndex, 0);
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1641,7 +1642,7 @@ usbd_req_set_hub_feature(struct usb_device *udev, struct lock *lock,
 	USETW(req.wValue, sel);
 	USETW(req.wIndex, 0);
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1663,7 +1664,7 @@ usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = port;
 	req.wIndex[1] = timeout;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1685,7 +1686,7 @@ usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = port;
 	req.wIndex[1] = timeout;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1706,7 +1707,7 @@ usbd_req_set_hub_depth(struct usb_device *udev, struct lock *lock,
 	USETW(req.wValue, depth);
 	USETW(req.wIndex, 0);
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1728,7 +1729,7 @@ usbd_req_clear_port_feature(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = port;
 	req.wIndex[1] = 0;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1750,7 +1751,7 @@ usbd_req_set_port_feature(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = port;
 	req.wIndex[1] = 0;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1779,7 +1780,7 @@ usbd_req_set_protocol(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
 	req.wIndex[1] = 0;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1863,7 +1864,7 @@ usbd_req_set_idle(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
 	req.wIndex[1] = 0;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -1918,7 +1919,7 @@ usbd_req_set_config(struct usb_device *udev, struct lock *lock, uint8_t conf)
 	req.wValue[1] = 0;
 	USETW(req.wIndex, 0);
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -2145,7 +2146,7 @@ usbd_req_clear_device_feature(struct usb_device *udev, struct lock *lock,
 	USETW(req.wValue, sel);
 	USETW(req.wIndex, 0);
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -2166,7 +2167,7 @@ usbd_req_set_device_feature(struct usb_device *udev, struct lock *lock,
 	USETW(req.wValue, sel);
 	USETW(req.wIndex, 0);
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -2194,7 +2195,7 @@ usbd_req_reset_tt(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = port;
 	req.wIndex[1] = 0;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -2228,7 +2229,7 @@ usbd_req_clear_tt_buffer(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = port;
 	req.wIndex[1] = 0;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
@@ -2252,7 +2253,7 @@ usbd_req_set_port_link_state(struct usb_device *udev, struct lock *lock,
 	req.wIndex[0] = port;
 	req.wIndex[1] = link_state;
 	USETW(req.wLength, 0);
-	return (usbd_do_request(udev, lock, &req, 0));
+	return (usbd_do_request(udev, lock, &req, NULL));
 }
 
 /*------------------------------------------------------------------------*
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index d8034b18e2..68e1ed273f 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -1528,7 +1528,7 @@ __elfN(putnote)(elf_buf_t target, const char *name, int type,
 		bcopy(name, dst, note.n_namesz);
 	target->off = roundup2(target->off, sizeof(Elf_Word));
 	dst = target_reserve(target, note.n_descsz, &error);
-	if (dst != NULL)
+	if (desc != NULL && dst != NULL)
 		bcopy(desc, dst, note.n_descsz);
 	target->off = roundup2(target->off, sizeof(Elf_Word));
 	return (error);
-- 
2.45.2

