From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Alperen Erkan <erkanalperen54@gmail.com>
Date: Sat, 25 Jul 2026 00:00:00 +0300
Subject: [PATCH] ipc_kmsg: assert non-NULL kmsg in ipc_kmsg_free

ipc_kmsg_free() dereferenced kmsg->ikm_size without any check that
kmsg was non-NULL. Any caller passing IKM_NULL would crash on that
dereference.

Rather than silently returning on a NULL kmsg -- which would hide a
real bug in the caller -- follow the convention already used
throughout this file (see ipc_kmsg_rmqueue, ipc_kmsg_copyout_dest,
etc.) and assert() the precondition, so a misbehaving caller is
caught loudly in debug/MACH_ASSERT builds instead of being papered
over.

No other behavioral changes.
---
 gnumach/ipc/ipc_kmsg.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/gnumach/ipc/ipc_kmsg.c b/gnumach/ipc/ipc_kmsg.c
index 8ab0e2e..b1c3fa1 100644
--- a/gnumach/ipc/ipc_kmsg.c
+++ b/gnumach/ipc/ipc_kmsg.c
@@ -449,7 +449,11 @@
 void
 ipc_kmsg_free(ipc_kmsg_t kmsg)
 {
-	vm_size_t size = kmsg->ikm_size;
+	vm_size_t size;
+
+	assert(kmsg != IKM_NULL);
+
+	size = kmsg->ikm_size;
 
 	switch (size) {
 
-- 
2.43.0
