]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: avoid new signed overflow warning on 32 bit
authorPádraig Brady <P@draigBrady.com>
Wed, 8 Oct 2014 11:35:36 +0000 (12:35 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 8 Oct 2014 11:48:51 +0000 (12:48 +0100)
Prompted by http://hydra.nixos.org/build/15682577
with GCC 4.8.3 on i686

  src/tac.c:557:6: error: assuming signed overflow does not occur
  when simplifying conditional to constant [-Werror=strict-overflow]
     if (bytes_copied < 0)

This happens because copy_to_temp() is inlined in tac_nonseekable(),
thus reducing the comparison to the bytes_copied variable in
copy_to_temp.  Now this can't overflow on either 32 or 64 bit
due to the protection of the preceding fwrite().  We could use a
guard like "if (bytes_copied <= OFF_T_MAX - bytes_read)" to avoid
the warning, but rather than a runtime branch, just use an unsigned
type to avoid apparent signed overflow on systems where the accumulation
is not promoted to unsigned (32 bit size_t, 64 bit off_t).

* src/tac.c (copy_to_temp): Increment an unsigned type to
avoid the subsequent signed overflow warning.

src/tac.c

index 248afa9d7873e5d6ee7f09dcf27f40d4b85e7fd6..777ec91932f2e8e3ee1bda56b8bde3cf963aae95 100644 (file)
--- a/src/tac.c
+++ b/src/tac.c
@@ -506,7 +506,7 @@ copy_to_temp (FILE **g_tmp, char **g_tempfile, int input_fd, char const *file)
 {
   FILE *fp;
   char *file_name;
-  off_t bytes_copied = 0;
+  uintmax_t bytes_copied = 0;
   if (!temp_stream (&fp, &file_name))
     return -1;
 
@@ -527,6 +527,9 @@ copy_to_temp (FILE **g_tmp, char **g_tempfile, int input_fd, char const *file)
           goto Fail;
         }
 
+      /* Implicitly <= OFF_T_MAX due to preceding fwrite(),
+         but unsigned type used to avoid compiler warnings
+         not aware of this fact.  */
       bytes_copied += bytes_read;
     }