Subject: Re: bug#11761: Slight bug in split :-)

From: Jim Meyering <[email-masked]>
Date: Fri, 22 Jun 2012 11:26:09 +0200

Pádraig Brady wrote:
> I'll push the attached in a while.
...
> @@ -360,10 +363,25 @@ create (const char *name)
>  {
>    if (!filter_command)
>      {
> +      int fd;
> +      struct stat out_stat_buf;
> +
>        if (verbose)
>          fprintf (stdout, _("creating file %s\n"), quote (name));
> -      return open (name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
> -                   (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
> +
> +      fd = open (name, O_WRONLY | O_CREAT | O_BINARY,
> +                 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
> +      if (fd < 0)
> +        return fd;
> +      if (fstat (fd, &out_stat_buf) != 0)
> +        error (EXIT_FAILURE, errno, _("failed to stat %s"), quote (name));
> +      if (SAME_INODE (in_stat_buf, out_stat_buf))
> +        error (EXIT_FAILURE, 0, _("%s would overwrite input; aborting"),
> +               quote (name));
> +      if (ftruncate (fd, 0) != 0)
> +        error (EXIT_FAILURE, errno, _("%s: error truncating"), quote (name));
> +
> +      return fd;

Thanks.
That looks fine and passes "make check syntax-check" (no surprise).
Please move the declaration of "fd" down to the point of initialization.

That would induce a line-split, but perhaps this is a good excuse to
factor out these:  (maybe name it something like MODE_RW_UGO)

    $ git grep -l 'S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH'
    src/dd.c
    src/mkfifo.c
    src/mknod.c
    src/split.c
    src/touch.c
    src/truncate.c