bug-tar
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-tar] address GCC8 build warnings


From: Jim Meyering
Subject: [Bug-tar] address GCC8 build warnings
Date: Mon, 19 Mar 2018 08:19:13 -0700

Thanks for adding zstd support.

Attempting to build the latest tar from git using GCC8 (built from git
itself) and configured with --enable-gcc-warnings showed multiple
errors. Here are some patches to address those:


>From f533689bfc23a040b18c4fdef25d7edc276237cb Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 18 Mar 2018 17:59:25 -0700
Subject: [PATCH 1/4] maint: avoid warnings from upcoming GCC8

* src/transform.c (_single_transform_name_to_obstack): Mark with
FALLTHROUGH statement rather than /* FALL THROUGH */ comment.
Only the former works with gcc-8.
* src/extract.c (maybe_recoverable): Call abort to tell gcc-8 that
this code is unreachable.
---
 src/extract.c   | 1 +
 src/transform.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/extract.c b/src/extract.c
index 395db55..74987bb 100644
--- a/src/extract.c
+++ b/src/extract.c
@@ -788,6 +788,7 @@ maybe_recoverable (char *file_name, bool regular, bool 
*interdir_made)
        case UNLINK_FIRST_OLD_FILES:
          break;
        }
+      abort (); /* notreached */

     case ENOENT:
       /* Attempt creating missing intermediate directories.  */
diff --git a/src/transform.c b/src/transform.c
index e450dd2..3fae3c0 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -550,7 +550,7 @@ _single_transform_name_to_obstack (struct transform *tf, 
char *input)
                        default:
                          break;
                        }
-                     /*FALL THROUGH*/
+                     FALLTHROUGH;

                    case ctl_upcase:
                    case ctl_locase:
-- 
2.16.1.72.g5be1f00a9


>From 5f6a698bfc47ae68e5361f96724fff26452d11f6 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 18 Mar 2018 19:21:51 -0700
Subject: [PATCH 2/4] maint: avoid -Wformat-overflow= warnings from upcoming
 GCC8
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* src/list.c: Include <assert.h>.
Add assertions to avoid this warning from gcc-8:
list.c:1052:38: error: ‘%02d’ directive writing between 2 and 11 \
  bytes into a region of size between 1 and 26 \
  [-Werror=format-overflow=]
---
 src/list.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/list.c b/src/list.c
index 14388a5..5c4fc52 100644
--- a/src/list.c
+++ b/src/list.c
@@ -23,6 +23,7 @@
 #include <system.h>
 #include <inttostr.h>
 #include <quotearg.h>
+#include <assert.h>

 #include "common.h"

@@ -1047,8 +1048,15 @@ tartime (struct timespec t, bool full_time)
   tm = utc_option ? gmtime (&s) : localtime (&s);
   if (tm)
     {
+      /* Tell static analysis tools that these numbers
+        are small enough to be formatted in BUFFER.  */
+      assert (0 <= tm->tm_mon && tm->tm_mon < 100);
+      assert (0 < tm->tm_mday && tm->tm_mday < 100);
+      assert (0 <= tm->tm_hour && tm->tm_hour < 100);
+      assert (0 <= tm->tm_min && tm->tm_min < 100);
       if (full_time)
        {
+         assert (0 <= tm->tm_sec && tm->tm_sec < 100);
          sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d",
                   tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
                   tm->tm_hour, tm->tm_min, tm->tm_sec);
-- 
2.16.1.72.g5be1f00a9


>From ee9cd85fc18109908102df38730fc26db26e824d Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 18 Mar 2018 21:20:28 -0700
Subject: [PATCH 3/4] maint: avoid -Wstringop-truncation warnings upcoming GCC8
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* src/buffer.c (gnu_add_multi_volume_header): Convert a use of
strncpy to memcpy, to avoid this warning:
In function ‘strncpy’,
    inlined from ‘gnu_add_multi_volume_header’ at buffer.c:1782:3,
    ...
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’\
   specified bound 100 equals destination size \
   [-Werror=stringop-truncation]
---
 src/buffer.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/buffer.c b/src/buffer.c
index 063e1be..b710c6a 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -1771,15 +1771,19 @@ gnu_add_multi_volume_header (struct bufmap *map)
 {
   int tmp;
   union block *block = find_next_block ();
+  size_t len = strlen (map->file_name);

-  if (strlen (map->file_name) > NAME_FIELD_SIZE)
-    WARN ((0, 0,
-           _("%s: file name too long to be stored in a GNU multivolume header, 
truncated"),
-           quotearg_colon (map->file_name)));
+  if (len > NAME_FIELD_SIZE)
+    {
+      WARN ((0, 0,
+            _("%s: file name too long to be stored in a GNU multivolume 
header, truncated"),
+            quotearg_colon (map->file_name)));
+      len = NAME_FIELD_SIZE;
+    }

   memset (block, 0, BLOCKSIZE);

-  strncpy (block->header.name, map->file_name, NAME_FIELD_SIZE);
+  memcpy (block->header.name, map->file_name, len);
   block->header.typeflag = GNUTYPE_MULTIVOL;

   OFF_TO_CHARS (map->sizeleft, block->header.size);
-- 
2.16.1.72.g5be1f00a9


>From 9266eafbfc4be6c7f6c9ed7dd59e905f78442938 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 18 Mar 2018 21:32:19 -0700
Subject: [PATCH 4/4] maint: avoid -Wstringop-truncation warnings from upcoming
 GCC8
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* src/create.c (start_private_header, start_header): Convert
trivial uses of strncpy to memcpy, to avoid warnings like this:
In function ‘strncpy’,
    inlined from ‘start_private_header’ at create.c:522:3:
/usr/include/bits/string_fortified.h:106:10: warning: \
  ‘__builtin_strncpy’ output truncated before terminating nul \
  copying 2 bytes from a string of the same length \
  [-Wstringop-truncation]
---
 src/create.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/create.c b/src/create.c
index 35bcf5b..7737c52 100644
--- a/src/create.c
+++ b/src/create.c
@@ -518,8 +518,8 @@ start_private_header (const char *name, size_t size, time_t 
t)
   MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
   UID_TO_CHARS (0, header->header.uid);
   GID_TO_CHARS (0, header->header.gid);
-  strncpy (header->header.magic, TMAGIC, TMAGLEN);
-  strncpy (header->header.version, TVERSION, TVERSLEN);
+  memcpy (header->header.magic, TMAGIC, TMAGLEN);
+  memcpy (header->header.version, TVERSION, TVERSLEN);
   return header;
 }

@@ -917,8 +917,8 @@ start_header (struct tar_stat_info *st)

     case POSIX_FORMAT:
     case USTAR_FORMAT:
-      strncpy (header->header.magic, TMAGIC, TMAGLEN);
-      strncpy (header->header.version, TVERSION, TVERSLEN);
+      memcpy (header->header.magic, TMAGIC, TMAGLEN);
+      memcpy (header->header.version, TVERSION, TVERSLEN);
       break;

     default:
-- 
2.16.1.72.g5be1f00a9




reply via email to

[Prev in Thread] Current Thread [Next in Thread]