]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
od: don’t assume no holes in wide unsigned
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 25 Jun 2025 03:01:04 +0000 (20:01 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 29 Jun 2025 04:00:41 +0000 (21:00 -0700)
Also, fix minor related typos.
* src/od.c (MAX_INTEGRAL_TYPE_SIZE, MAX_ADDRESS_LENGTH):
Now a constant, not a macro.
(MAX_INTEGRAL_TYPE_WIDTH): New constant.  Use it instead of
CHAR_BIT, so as not to assume that uintmax_t and unsigned long
long int are hole-free.  This doesn’t matter on practical porting
targets, though there is still a mainframe or two that have holes.
(FMT_BYTES_ALLOCATED): Fix typo by changing "jd" to "jo".
Fix off-by-one typo in static assertion.

src/od.c

index c1cdd310a49cc81c86c855c41f8e7fab2e24ea1e..8781a2258b67ff4d3e03c6dea1f037a6d3b8abcd 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -92,7 +92,8 @@ enum output_format
     CHARACTER
   };
 
-#define MAX_INTEGRAL_TYPE_SIZE sizeof (unsigned long long int)
+enum { MAX_INTEGRAL_TYPE_SIZE = sizeof (unsigned long long int) };
+enum { MAX_INTEGRAL_TYPE_WIDTH = ULLONG_WIDTH };
 
 /* The maximum number of bytes needed for a format string, including
    the trailing nul.  Each format string expects a variable amount of
@@ -104,13 +105,13 @@ enum
            (sizeof "%*.99" + 1
             + MAX (sizeof "ld",
                    MAX (sizeof "jd",
-                        MAX (sizeof "jd",
+                        MAX (sizeof "jo",
                              MAX (sizeof "ju",
                                   sizeof "jx")))))
   };
 
 /* Ensure that our choice for FMT_BYTES_ALLOCATED is reasonable.  */
-static_assert (MAX_INTEGRAL_TYPE_SIZE * CHAR_BIT / 3 <= 99);
+static_assert (MAX_INTEGRAL_TYPE_WIDTH <= 3 * 99);
 
 /* Each output format specification (from '-t spec' or from
    old-style options) is represented by one of these structures.  */
@@ -202,8 +203,7 @@ static int address_base;
 
 /* The number of octal digits required to represent the largest
    address value.  */
-#define MAX_ADDRESS_LENGTH \
-  ((sizeof (uintmax_t) * CHAR_BIT + CHAR_BIT - 1) / 3)
+enum { MAX_ADDRESS_LENGTH = UINTMAX_WIDTH / 3 + (UINTMAX_WIDTH % 3 != 0) };
 
 /* Width of a normal address.  */
 static int address_pad_len;