]> Savannah Git Hosting - gnulib.git/commitdiff
quotearg: pacify GCC better
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 15 Dec 2016 17:53:45 +0000 (09:53 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 15 Dec 2016 18:19:48 +0000 (10:19 -0800)
* 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
lib/quotearg.c
modules/quotearg

index 767e0c714e5c9c6c398e4e502a4876dbd8f75fd7..612d66ca731b8467229f53443d4180b8a2d2e9a3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2016-12-15  Paul Eggert  <eggert@cs.ucla.edu>
+
+       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  <eggert@cs.ucla.edu>
 
        xalloc-oversized: check for PTRDIFF_MAX too
index 730b4b38d7f5906d5a5aaef5b6864c22a658c8af..07658b24f5ccc72589d1af15cad23377d2c87305 100644 (file)
@@ -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 <errno.h>
 #include <limits.h>
 #include <stdbool.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <wchar.h>
@@ -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;
     }
 
   {
index bd63761e52c36d9c9fa4dd5e177728ba9738053b..109a16de54d39f77191201bbc4eefefee713f272 100644 (file)
@@ -16,9 +16,11 @@ gettext-h
 mbrtowc
 mbsinit
 memcmp
+minmax
 quotearg-simple
 localcharset
 stdbool
+stdint
 wchar
 wctype-h
 xalloc