[syslinux] [PATCH 4/4] extlinux: simplification by removing one label

nico cornu nicolac76 at yahoo.fr
Wed Nov 18 04:37:26 PST 2015


I agree that gotos can be useful to clean up complex environment. Here it's not one. close(dirfd) is only useful in one of three gotos.
 


    Le Mercredi 18 novembre 2015 7h33, Celelibi <celelibi at gmail.com> a écrit :
 

 2015-11-13 21:19 UTC+01:00, Nicolas Cornu via Syslinux <syslinux at zytor.com>:
> Now that xfs_install_file is simplified by using new helpers, we can
> remove use of label/goto for clarity.
>
> Signed-off-by: Nicolas Cornu <nicolac76 at yahoo.fr>
> ---
>  extlinux/main.c | 18 ++++++------------
>  1 file changed, 6 insertions(+), 12 deletions(-)
>
> diff --git a/extlinux/main.c b/extlinux/main.c
> index 43c1a31..8b97a71 100644
> --- a/extlinux/main.c
> +++ b/extlinux/main.c
> @@ -735,28 +735,22 @@ static int xfs_install_file(const char *path, int
> devfd, struct stat *rst)
>      dirfd = open(path, O_RDONLY | O_DIRECTORY);
>      if (dirfd < 0) {
>      perror(path);
> -    goto bail;
> +    return 1;
>      }
>
> -    if (ext_install_ldlinux_sys(devfd, path, rst))
> -    goto bail;
> +    if (ext_install_ldlinux_sys(devfd, path, rst)) {
> +    close(dirfd);
> +    return 1;
> +    }
>
>      close(dirfd);
>
> -    dirfd = -1;
> -
>      if (ext_install_ldlinux_c32(path))
> -    goto bail;
> +    return 1;
>
>      sync();
>
>      return 0;
> -
> -bail:
> -    if (dirfd >= 0)
> -    close(dirfd);
> -
> -    return 1;
>  }
>
>  /*
> --
> 2.6.2

I would just note that gotos are often used in C (at least in Linux)
as a a simulation of exceptions. The label is at the end of the
function doing some clean up and returning an error code. That's a
common pattern.

That being said, as your patch shorten the code length, I guess we can
say it's better.

Celelibi


  


More information about the Syslinux mailing list