Applied, thanks!

Mikhail Karpov, le ven. 20 nov. 2026 16:28:00 +0700, a ecrit:
> Hello,
> 
> On Thu, 19 Nov 2026 23:55:37 Samuel Thibault wrote:
> 
> > > diff --git a/libdiskfs/dir-unlink.c b/libdiskfs/dir-unlink.c
> > > index eaf62f9..3555d9e 100644
> > > --- a/libdiskfs/dir-unlink.c
> > > +++ b/libdiskfs/dir-unlink.c
> > > @@ -84,7 +84,7 @@ diskfs_S_dir_unlink (struct protid *dircred,
> > >    diskfs_node_update (np,  diskfs_synchronous);
> > >
> > >    if (np->dn_stat.st_nlink == 0)
> > > -    fshelp_fetch_control (&np->transbox, &control);
> > > +    err = fshelp_fetch_control (&np->transbox, &control);
> >
> > Similarly, we have already done some actions above.
> 
> I think we can simplify the code by factoring out the error handling return
> points.  We might also want to add a check for np == dnp after
> diskfs_dirremove returns an error.  If this check isn't necessary, we can
> simply proceed directly to the diskfs_nput(np) call.
> 
> > > diff --git a/libfshelp/fetch-root.c b/libfshelp/fetch-root.c
> > > index a7cee33..fcd83d4 100644
> > > --- a/libfshelp/fetch-root.c
> > > +++ b/libfshelp/fetch-root.c
> > > @@ -195,7 +195,11 @@ fshelp_fetch_root (struct transbox *box, void
> *cookie,
> > >
> > >    if ((err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
> > >        && control == box->active)
> > > -    fshelp_set_active (box, MACH_PORT_NULL, 0);
> > > +    {
> > > +      error_t err2 = fshelp_set_active (box, MACH_PORT_NULL, 0);
> > > +      if (err2)
> > > +        return err2;
> >
> > Here as well, we have a reference on control to release.
> 
> Yes, the control is now released before an error is returned.  I also
> believe
> it is very important to address two earlier instances in the code where
> mach_port_mod_refs is called without checking the result; however, I don’t
> think that simply returning an error is sufficient in those cases...
> 
> Thanks,
> 
> -- 
> Mikhail Karpov

> From 292a3313769125535aa4117e078745b21571a8dd Mon Sep 17 00:00:00 2001
> From: Mikhail Karpov <[email protected]>
> Date: Fri, 20 Nov 2026 16:04:03 +0700
> Subject: [PATCH 2/2] Added checks for libfshelp functions return value in
>  several place
> 
> ---
>  fatfs/dir.c            | 10 +++++----
>  libdiskfs/dir-unlink.c | 51 ++++++++++++++++++++++--------------------
>  libfshelp/fetch-root.c | 18 +++++++++++----
>  3 files changed, 47 insertions(+), 32 deletions(-)
> 
> diff --git a/fatfs/dir.c b/fatfs/dir.c
> index d210bcc..5496e36 100644
> --- a/fatfs/dir.c
> +++ b/fatfs/dir.c
> @@ -745,14 +745,16 @@ diskfs_dirrewrite_hard (struct node *dp, struct node 
> *np, struct dirstat *ds)
>  
>    assert_backtrace (!diskfs_readonly);
>  
> +  /* Close the file, free the referenced held by clients.  */
> +  err = fshelp_fetch_control (&oldnp->transbox, &control);
> +  if (err)
> +    return err;
> +
>    /*  The link count must be 0 so the file will be removed and
>        the node will be dropped.  */
>    oldnp->dn_stat.st_nlink--;
>    assert_backtrace (!oldnp->dn_stat.st_nlink);
> -  
> -  /* Close the file, free the referenced held by clients.  */
> -  fshelp_fetch_control (&oldnp->transbox, &control);
> -  
> +
>    if (control)
>      {
>        fsys_goaway (control, FSYS_GOAWAY_UNLINK);
> diff --git a/libdiskfs/dir-unlink.c b/libdiskfs/dir-unlink.c
> index eaf62f9..2591219 100644
> --- a/libdiskfs/dir-unlink.c
> +++ b/libdiskfs/dir-unlink.c
> @@ -26,7 +26,7 @@ diskfs_S_dir_unlink (struct protid *dircred,
>                    const_string_t name)
>  {
>    struct node *dnp;
> -  struct node *np;
> +  struct node *np = NULL;
>    struct dirstat *ds = alloca (diskfs_dirstat_size);
>    error_t err;
>    diskfs_transaction_t *txn;
> @@ -46,36 +46,26 @@ diskfs_S_dir_unlink (struct protid *dircred,
>    if (err == EAGAIN)
>      err = EPERM;     /* 1003.1-1996 5.5.1.4 */
>    if (err)
> -    {
> -      diskfs_drop_dirstat (dnp, ds);
> -      pthread_mutex_unlock (&dnp->lock);
> -      diskfs_journal_stop_transaction (txn);
> -      return err;
> -    }
> +    goto drop_dirstat;
>  
>    /* This isn't the BSD behavior, but it is Posix compliant and saves
>       us on several race conditions.*/
>    if (S_ISDIR(np->dn_stat.st_mode))
>      {
> -      if (np == dnp)         /* gotta catch '.' */
> -     diskfs_nrele (np);
> -      else
> -     diskfs_nput (np);
> -      diskfs_drop_dirstat (dnp, ds);
> -      pthread_mutex_unlock (&dnp->lock);
> -      diskfs_journal_stop_transaction (txn);
> -      return EPERM;          /* 1003.1-1996 5.5.1.4 */
> +      err = EPERM;
> +      goto drop_dirstat;
>      }
>  
> +  if (np->dn_stat.st_nlink == 1)
> +    err = fshelp_fetch_control (&np->transbox, &control);
> +
> +  if (err)
> +    goto drop_dirstat;
> +
>    err = diskfs_dirremove (dnp, np, name, ds);
>    diskfs_node_update (dnp, diskfs_synchronous);
>    if (err)
> -    {
> -      diskfs_nput (np);
> -      pthread_mutex_unlock (&dnp->lock);
> -      diskfs_journal_stop_transaction (txn);
> -      return err;
> -    }
> +    goto node_nrele;
>  
>    np->dn_stat.st_nlink--;
>    np->dn_set_ctime = 1;
> @@ -83,9 +73,6 @@ diskfs_S_dir_unlink (struct protid *dircred,
>      diskfs_orphan_add (np);
>    diskfs_node_update (np,  diskfs_synchronous);
>  
> -  if (np->dn_stat.st_nlink == 0)
> -    fshelp_fetch_control (&np->transbox, &control);
> -
>    /* This check is necessary because we might get here on an error while
>       checking the mode on something which happens to be `.'. */
>    if (np == dnp)
> @@ -101,6 +88,22 @@ diskfs_S_dir_unlink (struct protid *dircred,
>      }
>  
>    diskfs_journal_end_transaction (txn, 1, diskfs_synchronous);
> +  return 0;
> +
> + drop_dirstat:
> +  diskfs_drop_dirstat (dnp, ds);
>  
> + node_nrele:
> +  if (np)
> +    {
> +      if (np == dnp)         /* gotta catch '.' */
> +     diskfs_nrele (np);
> +      else
> +     diskfs_nput (np);
> +    }
> +
> + return_error:
> +  pthread_mutex_unlock (&dnp->lock);
> +  diskfs_journal_stop_transaction (txn);
>    return err;
>  }
> diff --git a/libfshelp/fetch-root.c b/libfshelp/fetch-root.c
> index a7cee33..c767f71 100644
> --- a/libfshelp/fetch-root.c
> +++ b/libfshelp/fetch-root.c
> @@ -54,7 +54,6 @@ fshelp_fetch_root (struct transbox *box, void *cookie,
>        uid_t uid, gid;
>        char *argz;
>        mach_msg_type_number_t argz_len;
> -      error_t err;
>        mach_port_t ports[INIT_PORT_MAX];
>        int ints[INIT_INT_MAX];
>        mach_port_t fds[STDERR_FILENO + 1];
> @@ -85,7 +84,9 @@ fshelp_fetch_root (struct transbox *box, void *cookie,
>         if (err)
>           ret = MACH_PORT_NULL;
>  
> -       mach_port_mod_refs (mach_task_self (), rend, MACH_PORT_RIGHT_RECEIVE, 
> -1);
> +       err = mach_port_mod_refs (mach_task_self (), rend,
> +                                 MACH_PORT_RIGHT_RECEIVE, -1);
> +       assert_perror_backtrace (err);
>  
>         return ret;
>       }
> @@ -181,9 +182,11 @@ fshelp_fetch_root (struct transbox *box, void *cookie,
>  
>    control = box->active;
>    /* FIXME: handle error properly */
> -  mach_port_mod_refs (mach_task_self (), control,
> +  err = mach_port_mod_refs (mach_task_self (), control,
>                           MACH_PORT_RIGHT_SEND, 1);
>    pthread_mutex_unlock (box->lock);
> +  if (err)
> +    return err;
>  
>    /* Cancellation point XXX */
>    err = fsys_getroot (control, dotdot, MACH_MSG_TYPE_COPY_SEND,
> @@ -195,7 +198,14 @@ fshelp_fetch_root (struct transbox *box, void *cookie,
>  
>    if ((err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
>        && control == box->active)
> -    fshelp_set_active (box, MACH_PORT_NULL, 0);
> +    {
> +      error_t err2 = fshelp_set_active (box, MACH_PORT_NULL, 0);
> +      if (err2)
> +     {
> +       mach_port_deallocate (mach_task_self (), control);
> +       return err2;
> +     }
> +    }
>    else
>      mach_port_deallocate (mach_task_self (), control);
>  
> -- 
> 2.43.0
> 


-- 
Samuel
<s> T'as pas de portable ?
<m> J'ai un nokia, dans le bassin d'arcachon

Reply via email to