From b94687cecbd44ff11c0c7f7f3d5dd50bcef5d9ac Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 16 Mar 1995 21:54:02 +0000 Subject: [PATCH] Detect when -N argument is too large to fit in an off_t. --- src/od.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/od.c b/src/od.c index 2165745..e8f9a52 100644 --- a/src/od.c +++ b/src/od.c @@ -71,6 +71,11 @@ typedef double LONG_DOUBLE; #ifndef ULONG_MAX #define ULONG_MAX ((unsigned long) ~(unsigned long) 0) #endif +#ifndef OFF_T_MAX +/* FIXME: is there a way to do this without relying on the + `8 bits per byte' assumption? */ +#define OFF_T_MAX (~((off_t)1 << (sizeof (off_t) * 8 - 1))) +#endif #define STREQ(a,b) (strcmp((a), (b)) == 0) @@ -126,12 +131,6 @@ enum output_format CHARACTER }; -enum strtoul_error - { - UINT_OK, UINT_INVALID, UINT_INVALID_SUFFIX_CHAR, UINT_OVERFLOW - }; -typedef enum strtoul_error strtoul_error; - /* Each output format specification (from POSIX `-t spec' or from old-style options) is represented by one of these structures. */ struct tspec @@ -1327,7 +1326,7 @@ parse_old_offset (s) { int radix; off_t offset; - strtol_error s_err; + enum strtol_error s_err; long unsigned int tmp; if (*s == '\0') @@ -1632,7 +1631,7 @@ main (argc, argv) != EOF) { unsigned long int tmp; - strtoul_error s_err; + enum strtol_error s_err; switch (c) { @@ -1680,9 +1679,16 @@ main (argc, argv) case 'N': limit_bytes_to_format = 1; - s_err = xstrtoul (optarg, NULL, 0, &max_bytes_to_format, "bkm"); + /* FIXME: if off_t is long long and that's an 8-byte type, + use xstrtouq here. */ + s_err = xstrtoul (optarg, NULL, 0, &tmp, "bkm"); + max_bytes_to_format = tmp; if (s_err != LONGINT_OK) STRTOL_FATAL_ERROR (optarg, "limit argument", s_err); + + if (tmp > OFF_T_MAX) + error (2, 0, "specified number of bytes `%s' is larger than \ +the maximum\nrepresentable value of type off_t", optarg); break; case 's': -- 2.7.4