Prefer < 0 to == -1 where either will do

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.
This commit is contained in:
Paul Eggert
2024-08-04 01:41:43 -07:00
parent 9cb1293628
commit 281e03ec6c
9 changed files with 45 additions and 43 deletions
+1 -1
View File
@@ -442,7 +442,7 @@ open_compressed_archive (void)
{ {
archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
MODE_RW, rsh_command_option); MODE_RW, rsh_command_option);
if (archive == -1) if (archive < 0)
return archive; return archive;
if (!multi_volume_option) if (!multi_volume_option)
+1 -1
View File
@@ -565,7 +565,7 @@ verify_volume (void)
ioctl (archive, FDFLUSH); ioctl (archive, FDFLUSH);
#endif #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. */ /* Lseek failed. Try a different method. */
seek_warn (archive_name_array[0]); seek_warn (archive_name_array[0]);
+1 -1
View File
@@ -85,7 +85,7 @@ info_attach_exclist (struct tar_stat_info *dir)
FILE *fp; FILE *fp;
struct exclude *ex = NULL; struct exclude *ex = NULL;
int fd = subfile_open (dir, file->name, O_RDONLY); int fd = subfile_open (dir, file->name, O_RDONLY);
if (fd == -1) if (fd < 0)
{ {
open_error (file->name); open_error (file->name);
continue; continue;
+1 -1
View File
@@ -1203,7 +1203,7 @@ pid_t
xfork (void) xfork (void)
{ {
pid_t p = fork (); pid_t p = fork ();
if (p == (pid_t) -1) if (p < 0)
call_arg_fatal ("fork", _("child process")); call_arg_fatal ("fork", _("child process"));
return p; return p;
} }
+2 -2
View File
@@ -301,7 +301,7 @@ sparse_scan_file_seek (struct tar_sparse_file *file)
/* locate first chunk of data */ /* locate first chunk of data */
data_offset = lseek (fd, offset, SEEK_DATA); data_offset = lseek (fd, offset, SEEK_DATA);
if (data_offset == (off_t)-1) if (data_offset < 0)
/* ENXIO == EOF; error otherwise */ /* ENXIO == EOF; error otherwise */
{ {
if (errno == ENXIO) if (errno == ENXIO)
@@ -1311,7 +1311,7 @@ pax_decode_header (struct tar_sparse_file *file)
FATAL_ERROR ((0, 0, _("Unexpected EOF in archive"))); FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
p = blk->buffer; p = blk->buffer;
COPY_BUF (blk,nbuf,p); 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"), ERROR ((0, 0, _("%s: malformed sparse archive member"),
file->stat_info->orig_file_name)); file->stat_info->orig_file_name));
+12 -11
View File
@@ -222,7 +222,7 @@ sys_wait_for_child (pid_t child_pid, bool eof)
{ {
int wait_status; int wait_status;
while (waitpid (child_pid, &wait_status, 0) == -1) while (waitpid (child_pid, &wait_status, 0) < 0)
if (errno != EINTR) if (errno != EINTR)
{ {
waitpid_error (use_compress_program_option); waitpid_error (use_compress_program_option);
@@ -258,7 +258,7 @@ sys_spawn_shell (void)
else else
{ {
int wait_status; int wait_status;
while (waitpid (child, &wait_status, 0) == -1) while (waitpid (child, &wait_status, 0) < 0)
if (errno != EINTR) if (errno != EINTR)
{ {
waitpid_error (shell); waitpid_error (shell);
@@ -343,7 +343,7 @@ wait_for_grandchild (pid_t pid)
int wait_status; int wait_status;
int exit_code = 0; int exit_code = 0;
while (waitpid (pid, &wait_status, 0) == -1) while (waitpid (pid, &wait_status, 0) < 0)
if (errno != EINTR) if (errno != EINTR)
{ {
waitpid_error (use_compress_program_option); waitpid_error (use_compress_program_option);
@@ -794,7 +794,7 @@ sys_wait_command (void)
return; return;
signal (SIGPIPE, pipe_handler); signal (SIGPIPE, pipe_handler);
while (waitpid (global_pid, &status, 0) == -1) while (waitpid (global_pid, &status, 0) < 0)
if (errno != EINTR) if (errno != EINTR)
{ {
global_pid = -1; 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') if (rc > 0 && buf[rc-1] == '\n')
buf[--rc] = 0; buf[--rc] = 0;
while (waitpid (pid, &status, 0) == -1) while (waitpid (pid, &status, 0) < 0)
if (errno != EINTR) if (errno != EINTR)
{ {
signal (SIGPIPE, saved_handler); signal (SIGPIPE, saved_handler);
@@ -906,7 +906,7 @@ sys_exec_checkpoint_script (const char *script_name,
int status; int status;
while (waitpid (pid, &status, 0) == -1) while (waitpid (pid, &status, 0) < 0)
if (errno != EINTR) if (errno != EINTR)
{ {
waitpid_error (script_name); waitpid_error (script_name);
@@ -988,7 +988,7 @@ sys_exec_setmtime_script (const char *script_name,
while (1) while (1)
{ {
int n = poll (&pfd, 1, -1); int n = poll (&pfd, 1, -1);
if (n == -1) if (n < 0)
{ {
if (errno != EINTR) if (errno != EINTR)
{ {
@@ -1007,14 +1007,14 @@ sys_exec_setmtime_script (const char *script_name,
bufsize = BUFSIZ; bufsize = BUFSIZ;
buffer = x2nrealloc (buffer, &bufsize, 1); buffer = x2nrealloc (buffer, &bufsize, 1);
} }
n = read (pfd.fd, buffer + buflen, bufsize - buflen); ssize_t nread = read (pfd.fd, buffer + buflen, bufsize - buflen);
if (n == -1) if (nread < 0)
{ {
ERROR ((0, errno, _("error reading output of %s"), script_name)); ERROR ((0, errno, _("error reading output of %s"), script_name));
stop = 1; stop = 1;
break; break;
} }
if (n == 0) if (nread == 0)
break; break;
buflen += n; buflen += n;
} }
@@ -1069,8 +1069,9 @@ sys_exec_setmtime_script (const char *script_name,
} }
else else
{ {
tm.tm_wday = -1;
t = mktime (&tm); t = mktime (&tm);
if (t == (time_t) -1) if (tm.tm_wday < 0)
{ {
ERROR ((0, errno, _("mktime failed"))); ERROR ((0, errno, _("mktime failed")));
rc = -1; rc = -1;
+2 -2
View File
@@ -508,8 +508,8 @@ _single_transform_name_to_obstack (struct transform *tf, char *input)
break; break;
case segm_backref: /* Back-reference segment */ case segm_backref: /* Back-reference segment */
if (rmp[segm->v.ref].rm_so != -1 if (0 <= rmp[segm->v.ref].rm_so
&& rmp[segm->v.ref].rm_eo != -1) && 0 <= rmp[segm->v.ref].rm_eo)
{ {
size_t size = rmp[segm->v.ref].rm_eo size_t size = rmp[segm->v.ref].rm_eo
- rmp[segm->v.ref].rm_so; - rmp[segm->v.ref].rm_so;
+22 -21
View File
@@ -316,7 +316,7 @@ xattrs__acls_set (struct tar_stat_info const *st,
return; 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 */ /* warn even if filesystem does not support acls */
WARNOPT (WARN_XATTR_WRITE, WARNOPT (WARN_XATTR_WRITE,
(0, errno, (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); int err = file_has_acl_at (parentfd, file_name, &st->stat);
if (err == 0) if (err == 0)
return; return;
if (err == -1) if (err < 0)
{ {
call_arg_warn ("file_has_acl_at", file_name); call_arg_warn ("file_has_acl_at", file_name);
return; return;
@@ -560,16 +560,16 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
if (!xatrs) if (!xatrs)
xatrs = x2nrealloc (xatrs, &xsz, 1); xatrs = x2nrealloc (xatrs, &xsz, 1);
while (((fd == 0) ? while (((xret = (fd == 0
((xret = ? listxattrat (parentfd, file_name, xatrs, xsz)
llistxattrat (parentfd, file_name, xatrs, xsz)) == -1) : : flistxattr (fd, xatrs, xsz)))
((xret = flistxattr (fd, xatrs, xsz)) == -1)) < 0)
&& (errno == ERANGE)) && errno == ERANGE)
{ {
xatrs = x2nrealloc (xatrs, &xsz, 1); xatrs = x2nrealloc (xatrs, &xsz, 1);
} }
if (xret == -1) if (xret < 0)
call_arg_warn ((fd == 0) ? "llistxattrat" : "flistxattr", file_name); call_arg_warn ((fd == 0) ? "llistxattrat" : "flistxattr", file_name);
else else
{ {
@@ -585,16 +585,17 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
size_t len = strlen (attr); size_t len = strlen (attr);
ssize_t aret = 0; ssize_t aret = 0;
while (((fd == 0) while (((aret = (fd == 0
? ((aret = lgetxattrat (parentfd, file_name, attr, ? lgetxattrat (parentfd, file_name, attr,
val, asz)) == -1) val, asz)
: ((aret = fgetxattr (fd, attr, val, asz)) == -1)) : fgetxattr (fd, attr, val, asz)))
&& (errno == ERANGE)) < 0)
&& errno == ERANGE)
{ {
val = x2nrealloc (val, &asz, 1); val = x2nrealloc (val, &asz, 1);
} }
if (aret != -1) if (0 <= aret)
{ {
if (!xattrs_masked_out (attr, true)) if (!xattrs_masked_out (attr, true))
xheader_xattr_add (st, attr, val, aret); xheader_xattr_add (st, attr, val, aret);
@@ -619,7 +620,7 @@ xattrs__fd_set (char const *file_name, char typeflag,
if (ptr) if (ptr)
{ {
const char *sysname = "setxattrat"; const char *sysname = "setxattrat";
int ret = -1; int ret;
if (typeflag != SYMTYPE) if (typeflag != SYMTYPE)
ret = setxattrat (chdir_fd, file_name, attr, ptr, len, 0); 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); ret = lsetxattrat (chdir_fd, file_name, attr, ptr, len, 0);
} }
if (ret == -1) if (ret < 0)
WARNOPT (WARN_XATTR_WRITE, WARNOPT (WARN_XATTR_WRITE,
(0, errno, (0, errno,
_("%s: Cannot set '%s' extended attribute for file '%s'"), _("%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"))); WARN ((0, 0, _("SELinux support is not available")));
done = 1; done = 1;
#else #else
int result = fd ? int result = (fd
fgetfilecon (fd, &st->cntx_name) ? fgetfilecon (fd, &st->cntx_name)
: lgetfileconat (parentfd, file_name, &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); call_arg_warn (fd ? "fgetfilecon" : "lgetfileconat", file_name);
#endif #endif
} }
@@ -691,7 +692,7 @@ xattrs_selinux_set (MAYBE_UNUSED struct tar_stat_info const *st,
sysname = "lsetfileconat"; sysname = "lsetfileconat";
} }
if (ret == -1) if (ret < 0)
WARNOPT (WARN_XATTR_WRITE, WARNOPT (WARN_XATTR_WRITE,
(0, errno, (0, errno,
_("%s: Cannot set SELinux context for file '%s'"), _("%s: Cannot set SELinux context for file '%s'"),
+3 -3
View File
@@ -59,11 +59,11 @@ check_seek_hole (int fd)
return EX_BAD; return EX_BAD;
offset = lseek (fd, 0, SEEK_DATA); offset = lseek (fd, 0, SEEK_DATA);
if (offset == (off_t)-1) if (offset < 0)
return EX_FAIL; return EX_FAIL;
offset = lseek (fd, offset, SEEK_HOLE); 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_FAIL;
return EX_OK; return EX_OK;
@@ -79,7 +79,7 @@ main ()
int rc; int rc;
char template[] = "testseekhole-XXXXXX"; char template[] = "testseekhole-XXXXXX";
int fd = mkstemp (template); int fd = mkstemp (template);
if (fd == -1) if (fd < 0)
return EX_BAD; return EX_BAD;
rc = check_seek_hole (fd); rc = check_seek_hole (fd);
close (fd); close (fd);