]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Prefer < 0 to == -1 where either will do
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 3 Aug 2024 05:11:13 +0000 (22:11 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 4 Aug 2024 08:41:43 +0000 (01:41 -0700)
Also, fix an unlikely read overflow in sys_exec_setmtime_script.
* src/buffer.c (open_compressed_archive):
* src/compare.c (verify_volume):
* src/exclist.c (info_attach_exclist):
* src/misc.c (xfork):
* src/sparse.c (sparse_scan_file_seek):
* src/system.c (sys_wait_for_child, sys_spawn_shell)
(wait_for_grandchild, sys_wait_command, sys_exec_info_script)
(sys_exec_checkpoint_script, sys_exec_setmtime_script):
* src/transform.c (_single_transform_name_to_obstack):
* src/xattrs.c (xattrs__acls_set, xattrs_acls_get)
(xattrs_xattrs_get, xattrs__fd_set, xattrs_selinux_get)
(xattrs_selinux_set):
* tests/checkseekhole.c (check_seek_hole, main):
Simplify failure tests by just looking at return value sign.
* src/system.c (sys_exec_setmtime_script):
Don’t assume ‘read’ result fits in int.
(sys_exec_setmtime_script): Don’t reject 1 second before Epoch.

src/buffer.c
src/compare.c
src/exclist.c
src/misc.c
src/sparse.c
src/system.c
src/transform.c
src/xattrs.c
tests/checkseekhole.c

index 9688a368c35d08bfeda7c2d117ea788e314f2b31..92d187c5455f7ad4d1b7f1f956e490b654d03e0d 100644 (file)
@@ -442,7 +442,7 @@ open_compressed_archive (void)
 {
   archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
                      MODE_RW, rsh_command_option);
-  if (archive == -1)
+  if (archive < 0)
     return archive;
 
   if (!multi_volume_option)
index 4b741c364e3b5c400bfed315e0f2adbed367fc03..d75e12ff8cf54d586e7b76ddb0dd97920a374252 100644 (file)
@@ -565,7 +565,7 @@ verify_volume (void)
   ioctl (archive, FDFLUSH);
 #endif
 
-  if (!mtioseek (true, -1) && rmtlseek (archive, 0, SEEK_SET) != 0)
+  if (!mtioseek (true, -1) && rmtlseek (archive, 0, SEEK_SET) < 0)
     {
       /* Lseek failed.  Try a different method.  */
       seek_warn (archive_name_array[0]);
index b4e0f9191741e6ffdbd55e43707110b2f19fee4f..ac70d0a527c1e6f6ec9557d8442e4a0684cfd4b3 100644 (file)
@@ -85,7 +85,7 @@ info_attach_exclist (struct tar_stat_info *dir)
          FILE *fp;
          struct exclude *ex = NULL;
          int fd = subfile_open (dir, file->name, O_RDONLY);
-         if (fd == -1)
+         if (fd < 0)
            {
              open_error (file->name);
              continue;
index da88c7e46cbebd94a2a2b9fc6fe0e535ce347358..e2483cd70b92d3a920f2411cc12edbecbb42339c 100644 (file)
@@ -1203,7 +1203,7 @@ pid_t
 xfork (void)
 {
   pid_t p = fork ();
-  if (p == (pid_t) -1)
+  if (p < 0)
     call_arg_fatal ("fork", _("child process"));
   return p;
 }
index 4ac4a40c10b44f84a0c78e82d3a68918609daca8..66d17d4dd20919604c8de3ada2ed75ee31672ef9 100644 (file)
@@ -301,7 +301,7 @@ sparse_scan_file_seek (struct tar_sparse_file *file)
       /* locate first chunk of data */
       data_offset = lseek (fd, offset, SEEK_DATA);
 
-      if (data_offset == (off_t)-1)
+      if (data_offset < 0)
         /* ENXIO == EOF; error otherwise */
         {
           if (errno == ENXIO)
@@ -1311,7 +1311,7 @@ pax_decode_header (struct tar_sparse_file *file)
         FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
       p = blk->buffer;
       COPY_BUF (blk,nbuf,p);
-      if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)))
+      if (!decode_num (&u, nbuf, SIZE_MAX))
        {
          ERROR ((0, 0, _("%s: malformed sparse archive member"),
                  file->stat_info->orig_file_name));
index 99cfa4035a50c6131df8376021d76474e704ceb1..2686d8ee3ccac22967001981396e031edfee97ed 100644 (file)
@@ -222,7 +222,7 @@ sys_wait_for_child (pid_t child_pid, bool eof)
     {
       int wait_status;
 
-      while (waitpid (child_pid, &wait_status, 0) == -1)
+      while (waitpid (child_pid, &wait_status, 0) < 0)
        if (errno != EINTR)
          {
            waitpid_error (use_compress_program_option);
@@ -258,7 +258,7 @@ sys_spawn_shell (void)
   else
     {
       int wait_status;
-      while (waitpid (child, &wait_status, 0) == -1)
+      while (waitpid (child, &wait_status, 0) < 0)
        if (errno != EINTR)
          {
            waitpid_error (shell);
@@ -343,7 +343,7 @@ wait_for_grandchild (pid_t pid)
   int wait_status;
   int exit_code = 0;
 
-  while (waitpid (pid, &wait_status, 0) == -1)
+  while (waitpid (pid, &wait_status, 0) < 0)
     if (errno != EINTR)
       {
        waitpid_error (use_compress_program_option);
@@ -794,7 +794,7 @@ sys_wait_command (void)
     return;
 
   signal (SIGPIPE, pipe_handler);
-  while (waitpid (global_pid, &status, 0) == -1)
+  while (waitpid (global_pid, &status, 0) < 0)
     if (errno != EINTR)
       {
         global_pid = -1;
@@ -849,7 +849,7 @@ sys_exec_info_script (const char **archive_name, int volume_number)
       if (rc > 0 && buf[rc-1] == '\n')
        buf[--rc] = 0;
 
-      while (waitpid (pid, &status, 0) == -1)
+      while (waitpid (pid, &status, 0) < 0)
        if (errno != EINTR)
          {
            signal (SIGPIPE, saved_handler);
@@ -906,7 +906,7 @@ sys_exec_checkpoint_script (const char *script_name,
 
       int status;
 
-      while (waitpid (pid, &status, 0) == -1)
+      while (waitpid (pid, &status, 0) < 0)
        if (errno != EINTR)
          {
            waitpid_error (script_name);
@@ -988,7 +988,7 @@ sys_exec_setmtime_script (const char *script_name,
   while (1)
     {
       int n = poll (&pfd, 1, -1);
-      if (n == -1)
+      if (n < 0)
        {
          if (errno != EINTR)
            {
@@ -1007,14 +1007,14 @@ sys_exec_setmtime_script (const char *script_name,
                bufsize = BUFSIZ;
              buffer = x2nrealloc (buffer, &bufsize, 1);
            }
-         n = read (pfd.fd, buffer + buflen, bufsize - buflen);
-         if (n == -1)
+         ssize_t nread = read (pfd.fd, buffer + buflen, bufsize - buflen);
+         if (nread < 0)
            {
              ERROR ((0, errno, _("error reading output of %s"), script_name));
              stop = 1;
              break;
            }
-         if (n == 0)
+         if (nread == 0)
            break;
          buflen += n;
        }
@@ -1069,8 +1069,9 @@ sys_exec_setmtime_script (const char *script_name,
        }
       else
        {
+         tm.tm_wday = -1;
          t = mktime (&tm);
-         if (t == (time_t) -1)
+         if (tm.tm_wday < 0)
            {
              ERROR ((0, errno, _("mktime failed")));
              rc = -1;
index 33a0f3cbb3003a89228ee2affbafb8c277142702..f9947193fe33df45818613072798d6a6bc45ee67 100644 (file)
@@ -508,8 +508,8 @@ _single_transform_name_to_obstack (struct transform *tf, char *input)
                  break;
 
                case segm_backref:    /* Back-reference segment */
-                 if (rmp[segm->v.ref].rm_so != -1
-                     && rmp[segm->v.ref].rm_eo != -1)
+                 if (0 <= rmp[segm->v.ref].rm_so
+                     && 0 <= rmp[segm->v.ref].rm_eo)
                    {
                      size_t size = rmp[segm->v.ref].rm_eo
                                      - rmp[segm->v.ref].rm_so;
index d99947c02a5ff147536dc9f6e5562bc5bef9b2af..c8a6588bcd2a2974a88faf68a116f2ad1e495f13 100644 (file)
@@ -316,7 +316,7 @@ xattrs__acls_set (struct tar_stat_info const *st,
       return;
     }
 
-  if (acl_set_file_at (chdir_fd, file_name, type, acl) == -1)
+  if (acl_set_file_at (chdir_fd, file_name, type, acl) < 0)
     /* warn even if filesystem does not support acls */
     WARNOPT (WARN_XATTR_WRITE,
             (0, errno,
@@ -462,7 +462,7 @@ xattrs_acls_get (MAYBE_UNUSED int parentfd, MAYBE_UNUSED char const *file_name,
       int err = file_has_acl_at (parentfd, file_name, &st->stat);
       if (err == 0)
         return;
-      if (err == -1)
+      if (err < 0)
         {
           call_arg_warn ("file_has_acl_at", file_name);
           return;
@@ -560,16 +560,16 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
       if (!xatrs)
        xatrs = x2nrealloc (xatrs, &xsz, 1);
 
-      while (((fd == 0) ?
-              ((xret =
-                llistxattrat (parentfd, file_name, xatrs, xsz)) == -1) :
-             ((xret = flistxattr (fd, xatrs, xsz)) == -1))
-             && (errno == ERANGE))
+      while (((xret = (fd == 0
+                      ? listxattrat (parentfd, file_name, xatrs, xsz)
+                      : flistxattr (fd, xatrs, xsz)))
+             < 0)
+             && errno == ERANGE)
         {
          xatrs = x2nrealloc (xatrs, &xsz, 1);
         }
 
-      if (xret == -1)
+      if (xret < 0)
         call_arg_warn ((fd == 0) ? "llistxattrat" : "flistxattr", file_name);
       else
         {
@@ -585,16 +585,17 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
               size_t len = strlen (attr);
               ssize_t aret = 0;
 
-              while (((fd == 0)
-                      ? ((aret = lgetxattrat (parentfd, file_name, attr,
-                                              val, asz)) == -1)
-                      : ((aret = fgetxattr (fd, attr, val, asz)) == -1))
-                     && (errno == ERANGE))
+             while (((aret = (fd == 0
+                              ? lgetxattrat (parentfd, file_name, attr,
+                                             val, asz)
+                              : fgetxattr (fd, attr, val, asz)))
+                     < 0)
+                     && errno == ERANGE)
                 {
                  val = x2nrealloc (val, &asz, 1);
                 }
 
-              if (aret != -1)
+              if (0 <= aret)
                 {
                   if (!xattrs_masked_out (attr, true))
                     xheader_xattr_add (st, attr, val, aret);
@@ -619,7 +620,7 @@ xattrs__fd_set (char const *file_name, char typeflag,
   if (ptr)
     {
       const char *sysname = "setxattrat";
-      int ret = -1;
+      int ret;
 
       if (typeflag != SYMTYPE)
         ret = setxattrat (chdir_fd, file_name, attr, ptr, len, 0);
@@ -629,7 +630,7 @@ xattrs__fd_set (char const *file_name, char typeflag,
           ret = lsetxattrat (chdir_fd, file_name, attr, ptr, len, 0);
         }
 
-      if (ret == -1)
+      if (ret < 0)
         WARNOPT (WARN_XATTR_WRITE,
                 (0, errno,
                  _("%s: Cannot set '%s' extended attribute for file '%s'"),
@@ -652,11 +653,11 @@ xattrs_selinux_get (MAYBE_UNUSED int parentfd, MAYBE_UNUSED char const *file_nam
         WARN ((0, 0, _("SELinux support is not available")));
       done = 1;
 #else
-      int result = fd ?
-                   fgetfilecon (fd, &st->cntx_name)
-                    : lgetfileconat (parentfd, file_name, &st->cntx_name);
+      int result = (fd
+                   fgetfilecon (fd, &st->cntx_name)
+                   : lgetfileconat (parentfd, file_name, &st->cntx_name));
 
-      if (result == -1 && errno != ENODATA && errno != ENOTSUP)
+      if (result < 0 && errno != ENODATA && errno != ENOTSUP)
         call_arg_warn (fd ? "fgetfilecon" : "lgetfileconat", file_name);
 #endif
     }
@@ -691,7 +692,7 @@ xattrs_selinux_set (MAYBE_UNUSED struct tar_stat_info const *st,
           sysname = "lsetfileconat";
         }
 
-      if (ret == -1)
+      if (ret < 0)
         WARNOPT (WARN_XATTR_WRITE,
                 (0, errno,
                  _("%s: Cannot set SELinux context for file '%s'"),
index 907d5d920ee3c22f607db9780ce81ef524721898..314250f62066bca99c1b108a89b1c77c9055c2a3 100644 (file)
@@ -59,11 +59,11 @@ check_seek_hole (int fd)
     return EX_BAD;
 
   offset = lseek (fd, 0, SEEK_DATA);
-  if (offset == (off_t)-1)
+  if (offset < 0)
     return EX_FAIL;
 
   offset = lseek (fd, offset, SEEK_HOLE);
-  if (offset == (off_t)-1 || offset == stat.st_size)
+  if (offset < 0 || offset == stat.st_size)
     return EX_FAIL;
 
   return EX_OK;
@@ -79,7 +79,7 @@ main ()
   int rc;
   char template[] = "testseekhole-XXXXXX";
   int fd = mkstemp (template);
-  if (fd == -1)
+  if (fd < 0)
     return EX_BAD;
   rc = check_seek_hole (fd);
   close (fd);