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-02 22:11:13 -07:00
parent 9cb1293628
commit 281e03ec6c
9 changed files with 45 additions and 43 deletions

View 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)

View 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]);

View 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;

View 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;
}

View 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));

View 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;

View 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;

View 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'"),

View 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);