]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
maint: use static_assert
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 30 Jul 2024 23:19:35 +0000 (16:19 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 4 Aug 2024 08:41:43 +0000 (01:41 -0700)
* gnulib.modules: Add assert-h, for static_assert.
* src/common.h, src/list.c, src/misc.c:
Prefer static_assert to #if + #error.  This doesn’t fix any bugs; it’s
just that in general it’s better to avoid the preprocessor.

gnulib.modules
src/common.h
src/list.c
src/misc.c

index b31c0dfc73ea8bec0e894d7d2d8efdfe00e8784d..8c4e8e8e38ac100a230442e034e34639ca1a50b2 100644 (file)
@@ -22,6 +22,7 @@ areadlinkat-with-size
 argmatch
 argp
 argp-version-etc
+assert-h
 attribute
 backupfile
 c-ctype
index 12721760af1d5bf0d4d9bb884cd23c1e149878b2..e0528fd509f10bbc7006e3440d9c0aa55747fb1e 100644 (file)
@@ -664,12 +664,11 @@ const char *tar_dirname (void);
 /* Represent N using a signed integer I such that (uintmax_t) I == N.
    With a good optimizing compiler, this is equivalent to (intmax_t) i
    and requires zero machine instructions.  */
-#if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
-# error "represent_uintmax returns intmax_t to represent uintmax_t"
-#endif
 COMMON_INLINE intmax_t
 represent_uintmax (uintmax_t n)
 {
+  static_assert (UINTMAX_MAX / 2 <= INTMAX_MAX);
+
   if (n <= INTMAX_MAX)
     return n;
   else
index e6a5aa60c840c7beda31dc950978395d649204e1..caacfd88d80db2b506c175f628a766ace78c2b25 100644 (file)
@@ -746,17 +746,17 @@ decode_header (union block *header, struct tar_stat_info *stat_info,
    (uintmax_t) V yields the correct result.  If OCTAL_ONLY, allow only octal
    numbers instead of the other GNU extensions.  Return -1 on error,
    diagnosing the error if TYPE is nonnull and if !SILENT.  */
-#if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
-# error "from_header internally represents intmax_t as uintmax_t + sign"
-#endif
-#if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
-# error "from_header returns intmax_t to represent uintmax_t"
-#endif
 static intmax_t
 from_header (char const *where0, size_t digs, char const *type,
             intmax_t minval, uintmax_t maxval,
             bool octal_only, bool silent)
 {
+  /* from_header internally represents intmax_t as uintmax_t + sign.  */
+  static_assert (INTMAX_MAX <= UINTMAX_MAX
+                && - (INTMAX_MIN + 1) <= UINTMAX_MAX);
+  /* from_header returns intmax_t to represent uintmax_t.  */
+  static_assert (UINTMAX_MAX / 2 <= INTMAX_MAX);
+
   uintmax_t value;
   uintmax_t uminval = minval;
   uintmax_t minus_minval = - uminval;
@@ -797,8 +797,7 @@ from_header (char const *where0, size_t digs, char const *type,
          value += *where++ - '0';
          if (where == lim || ! is_octal_digit (*where))
            break;
-         overflow |= value != (value << LG_8 >> LG_8);
-         value <<= LG_8;
+         overflow |= ckd_mul (&value, value, 8);
        }
 
       /* Parse the output of older, unportable tars, which generate
index 2c5dec9a9926b5304ce331a439a1c64e1a708349..1e2e8f3f3e314d971c8987df849e89783d78e9f4 100644 (file)
@@ -377,13 +377,12 @@ replace_prefix (char **pname, const char *samp, size_t slen,
    converted string.  If VALUE is converted from a negative integer in
    the range MINVAL .. -1, represent it with a string representation
    of the negative integer, using leading '-'.  */
-#if ! (INTMAX_MAX <= UINTMAX_MAX / 2)
-# error "sysinttostr: uintmax_t cannot represent all intmax_t values"
-#endif
 char *
 sysinttostr (uintmax_t value, intmax_t minval, uintmax_t maxval,
             char buf[SYSINT_BUFSIZE])
 {
+  static_assert (INTMAX_MAX <= UINTMAX_MAX / 2);
+
   if (value <= maxval)
     return umaxtostr (value, buf);
   else
@@ -406,12 +405,11 @@ sysinttostr (uintmax_t value, intmax_t minval, uintmax_t maxval,
    On a normal return, set errno = 0.
    On conversion error, return 0 and set errno = EINVAL.
    On overflow, return an extreme value and set errno = ERANGE.  */
-#if ! (INTMAX_MAX <= UINTMAX_MAX)
-# error "strtosysint: nonnegative intmax_t does not fit in uintmax_t"
-#endif
 intmax_t
 strtosysint (char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
 {
+  static_assert (INTMAX_MAX <= UINTMAX_MAX);
+
   errno = 0;
   if (maxval <= INTMAX_MAX)
     {