From cb4e027f61170badd1d856a60a1c02a01edf3bda Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 15 Dec 2016 09:53:45 -0800 Subject: [PATCH] quotearg: pacify GCC better * modules/quotearg (Depends-on): Add minmax, stdint. * lib/quotearg.c: Include minmax.h, stdint.h. (nslots): Now int, as there seems little point to going to extra work merely to support the INT_MAX slot, which nobody ever uses. (quotearg_n_options): Redo size-overflow checks to pacify GCC and to catch (mostly-theoretical) ptrdiff_t problems too. This can be done via one comparison. --- ChangeLog | 11 +++++++++++ lib/quotearg.c | 23 +++++++++-------------- modules/quotearg | 2 ++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 767e0c714e..612d66ca73 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2016-12-15 Paul Eggert + + quotearg: pacify GCC better + * modules/quotearg (Depends-on): Add minmax, stdint. + * lib/quotearg.c: Include minmax.h, stdint.h. + (nslots): Now int, as there seems little point to going to extra + work merely to support the INT_MAX slot, which nobody ever uses. + (quotearg_n_options): Redo size-overflow checks to pacify GCC + and to catch (mostly-theoretical) ptrdiff_t problems too. + This can be done via one comparison. + 2016-12-14 Paul Eggert xalloc-oversized: check for PTRDIFF_MAX too diff --git a/lib/quotearg.c b/lib/quotearg.c index 730b4b38d7..07658b24f5 100644 --- a/lib/quotearg.c +++ b/lib/quotearg.c @@ -29,6 +29,7 @@ #include "quotearg.h" #include "quote.h" +#include "minmax.h" #include "xalloc.h" #include "c-strcaseeq.h" #include "localcharset.h" @@ -37,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -830,7 +832,7 @@ struct slotvec /* Preallocate a slot 0 buffer, so that the caller can always quote one small component of a "memory exhausted" message in slot 0. */ static char slot0[256]; -static unsigned int nslots = 1; +static int nslots = 1; static struct slotvec slotvec0 = {sizeof slot0, slot0}; static struct slotvec *slotvec = &slotvec0; @@ -838,7 +840,7 @@ void quotearg_free (void) { struct slotvec *sv = slotvec; - unsigned int i; + int i; for (i = 1; i < nslots; i++) free (sv[i].val); if (sv[0].val != slot0) @@ -869,30 +871,23 @@ quotearg_n_options (int n, char const *arg, size_t argsize, { int e = errno; - unsigned int n0 = n; struct slotvec *sv = slotvec; if (n < 0) abort (); - if (nslots <= n0) + if (nslots <= n) { - /* FIXME: technically, the type of n1 should be 'unsigned int', - but that evokes an unsuppressible warning from gcc-4.0.1 and - older. If gcc ever provides an option to suppress that warning, - revert to the original type, so that the test in xalloc_oversized - is once again performed only at compile time. */ - size_t n1 = n0 + 1; bool preallocated = (sv == &slotvec0); - if (xalloc_oversized (n1, sizeof *sv)) + if (MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) <= n) xalloc_die (); - slotvec = sv = xrealloc (preallocated ? NULL : sv, n1 * sizeof *sv); + slotvec = sv = xrealloc (preallocated ? NULL : sv, (n + 1) * sizeof *sv); if (preallocated) *sv = slotvec0; - memset (sv + nslots, 0, (n1 - nslots) * sizeof *sv); - nslots = n1; + memset (sv + nslots, 0, (n + 1 - nslots) * sizeof *sv); + nslots = n + 1; } { diff --git a/modules/quotearg b/modules/quotearg index bd63761e52..109a16de54 100644 --- a/modules/quotearg +++ b/modules/quotearg @@ -16,9 +16,11 @@ gettext-h mbrtowc mbsinit memcmp +minmax quotearg-simple localcharset stdbool +stdint wchar wctype-h xalloc -- 2.39.5