Hello,

On Thu, 29 Oct 2026 00:07:46 Samuel Thibault wrote:

> > +      err = mach_port_mod_refs (mach_task_self (), transbox->active,
> > +                             MACH_PORT_RIGHT_SEND, +1);
> > +      if (err)
> > +     {
> > +       error_t request_err =
> > +         mach_port_request_notification (mach_task_self (),
> > +  transbox->active,
> > +  MACH_NOTIFY_DEAD_NAME, 0,
> > +  MACH_PORT_NULL,
> > +  MACH_MSG_TYPE_MAKE_SEND_ONCE,
> > +                                         &old);
> > +       assert_perror_backtrace (request_err);
>
> I believe you'd have to deallocate the old port.
>
> > +       goto out;
> > +     }
> > +
> >        if (MACH_PORT_VALID (old))
> >       mach_port_deallocate (mach_task_self (), old);
>
> This also needs to be done anyway, even if err != 0.

I think we could call mach_port_deallocate (mach_task_self (), old)
immediately after the call to mach_port_request_notification.

Thanks,

--
Mikhail Karpov
From 076124eaa49ac852ef24433cb929e87a1c74ac1e Mon Sep 17 00:00:00 2001
From: Mikhail Karpov <[email protected]>
Date: Thu, 29 Oct 2026 11:45:19 +0700
Subject: [PATCH 2/2] Added checks for mach_port_mod_refs return value in
 several place

---
 libfshelp/translator-list.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/libfshelp/translator-list.c b/libfshelp/translator-list.c
index 6d4f941..352156a 100644
--- a/libfshelp/translator-list.c
+++ b/libfshelp/translator-list.c
@@ -128,17 +128,38 @@ fshelp_set_active_translator (struct port_info *pi,
  update:
   if (MACH_PORT_VALID (transbox->active) && transbox->active != t->active)
     {
-      mach_port_t old;
-      err = mach_port_request_notification (mach_task_self (), transbox->active,
+      mach_port_t old = MACH_PORT_NULL;
+      err = mach_port_request_notification (mach_task_self (),
+					    transbox->active,
 					    MACH_NOTIFY_DEAD_NAME, 0,
 					    pi->port_right,
 					    MACH_MSG_TYPE_MAKE_SEND_ONCE,
 					    &old);
-      if (err)
-	goto out;
       if (MACH_PORT_VALID (old))
 	mach_port_deallocate (mach_task_self (), old);
 
+      if (err)
+	goto out;
+
+      err = mach_port_mod_refs (mach_task_self (), transbox->active,
+				MACH_PORT_RIGHT_SEND, +1);
+      if (err)
+	{
+	  old = MACH_PORT_NULL;
+	  error_t request_err =
+	    mach_port_request_notification (mach_task_self (),
+					    transbox->active,
+					    MACH_NOTIFY_DEAD_NAME, 0,
+					    MACH_PORT_NULL,
+					    MACH_MSG_TYPE_MAKE_SEND_ONCE,
+					    &old);
+	  assert_perror_backtrace (request_err);
+	  if (MACH_PORT_VALID (old))
+	    mach_port_deallocate (mach_task_self (), old);
+
+          goto out;
+	}
+
       if (t->pi)
 	ports_port_deref (t->pi);
 
@@ -147,8 +168,7 @@ fshelp_set_active_translator (struct port_info *pi,
 
       if (MACH_PORT_VALID (t->active))
 	mach_port_deallocate (mach_task_self (), t->active);
-      mach_port_mod_refs (mach_task_self (), transbox->active,
-			  MACH_PORT_RIGHT_SEND, +1);
+
       t->active = transbox->active;
     }
   else if (! MACH_PORT_VALID (transbox->active))
-- 
2.43.0

Reply via email to