* lib/quotearg.h (quotearg_n, quotearg_n_style):

First arg is int, not unsigned.
* lib/quotearg.c (quotearg_n, quotearg_n_style): Likewise.
(SIZE_MAX, UINT_MAX): New macros.
(quotearg_n_options): Abort if N is negative.
Avoid overflow check on hosts where size_t is 64 bits and int
is 32 bits, as overflow is impossible there.
Fix off-by-one typo that caused unnecessary reallocation.
This commit is contained in:
Akim Demaille
2001-11-30 14:04:24 +00:00
parent 7093d0f541
commit f4e421e6e4
3 changed files with 36 additions and 13 deletions

View File

@@ -1,3 +1,14 @@
2001-11-27 Paul Eggert <eggert@twinsun.com>
* lib/quotearg.h (quotearg_n, quotearg_n_style):
First arg is int, not unsigned.
* lib/quotearg.c (quotearg_n, quotearg_n_style): Likewise.
(SIZE_MAX, UINT_MAX): New macros.
(quotearg_n_options): Abort if N is negative.
Avoid overflow check on hosts where size_t is 64 bits and int
is 32 bits, as overflow is impossible there.
Fix off-by-one typo that caused unnecessary reallocation.
2001-11-29 Paul Eggert <eggert@twinsun.com> 2001-11-29 Paul Eggert <eggert@twinsun.com>
Name space cleanup in generated parser. Name space cleanup in generated parser.

View File

@@ -44,9 +44,15 @@
#ifndef CHAR_BIT #ifndef CHAR_BIT
# define CHAR_BIT 8 # define CHAR_BIT 8
#endif #endif
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t) -1)
#endif
#ifndef UCHAR_MAX #ifndef UCHAR_MAX
# define UCHAR_MAX ((unsigned char) -1) # define UCHAR_MAX ((unsigned char) -1)
#endif #endif
#ifndef UINT_MAX
# define UINT_MAX ((unsigned int) -1)
#endif
#if HAVE_C_BACKSLASH_A #if HAVE_C_BACKSLASH_A
# define ALERT_CHAR '\a' # define ALERT_CHAR '\a'
@@ -539,6 +545,7 @@ quotearg_n_options (int n, char const *arg,
one small component of a "memory exhausted" message in slot 0. */ one small component of a "memory exhausted" message in slot 0. */
static char slot0[256]; static char slot0[256];
static unsigned int nslots = 1; static unsigned int nslots = 1;
unsigned int n0 = n;
struct slotvec struct slotvec
{ {
size_t size; size_t size;
@@ -547,20 +554,26 @@ quotearg_n_options (int n, char const *arg,
static struct slotvec slotvec0 = {sizeof slot0, slot0}; static struct slotvec slotvec0 = {sizeof slot0, slot0};
static struct slotvec *slotvec = &slotvec0; static struct slotvec *slotvec = &slotvec0;
if (nslots <= n) if (n < 0)
abort ();
if (nslots <= n0)
{ {
int n1 = n + 1; unsigned int n1 = n0 + 1;
size_t s = n1 * sizeof (struct slotvec); size_t s = n1 * sizeof *slotvec;
if (! (0 < n1 && n1 == s / sizeof (struct slotvec)))
abort (); if (SIZE_MAX / UINT_MAX <= sizeof *slotvec
&& n1 != s / sizeof *slotvec)
xalloc_die ();
if (slotvec == &slotvec0) if (slotvec == &slotvec0)
{ {
slotvec = (struct slotvec *) xmalloc (sizeof (struct slotvec)); slotvec = (struct slotvec *) xmalloc (sizeof *slotvec);
*slotvec = slotvec0; *slotvec = slotvec0;
} }
slotvec = (struct slotvec *) xrealloc (slotvec, s); slotvec = (struct slotvec *) xrealloc (slotvec, s);
memset (slotvec + nslots, 0, (n1 - nslots) * sizeof (struct slotvec)); memset (slotvec + nslots, 0, (n1 - nslots) * sizeof *slotvec);
nslots = n; nslots = n1;
} }
{ {
@@ -580,7 +593,7 @@ quotearg_n_options (int n, char const *arg,
} }
char * char *
quotearg_n (unsigned int n, char const *arg) quotearg_n (int n, char const *arg)
{ {
return quotearg_n_options (n, arg, &default_quoting_options); return quotearg_n_options (n, arg, &default_quoting_options);
} }
@@ -592,7 +605,7 @@ quotearg (char const *arg)
} }
char * char *
quotearg_n_style (unsigned int n, enum quoting_style s, char const *arg) quotearg_n_style (int n, enum quoting_style s, char const *arg)
{ {
struct quoting_options o; struct quoting_options o;
o.style = s; o.style = s;

View File

@@ -89,7 +89,7 @@ size_t quotearg_buffer PARAMS ((char *buffer, size_t buffersize,
The returned value points to static storage that can be The returned value points to static storage that can be
reused by the next call to this function with the same value of N. reused by the next call to this function with the same value of N.
N must be nonnegative. */ N must be nonnegative. */
char *quotearg_n PARAMS ((unsigned int n, char const *arg)); char *quotearg_n PARAMS ((int n, char const *arg));
/* Equivalent to quotearg_n (0, ARG). */ /* Equivalent to quotearg_n (0, ARG). */
char *quotearg PARAMS ((char const *arg)); char *quotearg PARAMS ((char const *arg));
@@ -97,8 +97,7 @@ char *quotearg PARAMS ((char const *arg));
/* Use style S and storage slot N to return a quoted version of the string ARG. /* Use style S and storage slot N to return a quoted version of the string ARG.
This is like quotearg_n (N, ARG), except that it uses S with no other This is like quotearg_n (N, ARG), except that it uses S with no other
options to specify the quoting method. */ options to specify the quoting method. */
char *quotearg_n_style PARAMS ((unsigned int n, enum quoting_style s, char *quotearg_n_style PARAMS ((int n, enum quoting_style s, char const *arg));
char const *arg));
/* Equivalent to quotearg_n_style (0, S, ARG). */ /* Equivalent to quotearg_n_style (0, S, ARG). */
char *quotearg_style PARAMS ((enum quoting_style s, char const *arg)); char *quotearg_style PARAMS ((enum quoting_style s, char const *arg));