bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] Re: extend the scope of xasprintf


From: Bruno Haible
Subject: Re: [bug-gnulib] Re: extend the scope of xasprintf
Date: Tue, 9 May 2006 19:16:01 +0200
User-agent: KMail/1.5

Paul Eggert wrote:
> totalsize is of type size_t, so SIZE_MAX is the problem case here, not
> INT_MAX.

Good point. Also a possible overflow in the xsum() calls before is needed.

I'm committing this changed code.

Bruno



static inline char *
xstrcat (size_t argcount, va_list args)
{
  char *result;
  va_list ap;
  size_t totalsize;
  size_t i;
  char *p;

  /* Determine the total size.  */
  totalsize = 0;
  va_copy (ap, args);
  for (i = argcount; i > 0; i--)
    {
      const char *next = va_arg (ap, const char *);
      totalsize = xsum (totalsize, strlen (next));
    }
  va_end (ap);

  /* Test for overflow in the summing pass above or in (totalsize + 1) below.
     Also, don't return a string longer than INT_MAX, for consistency with
     vasprintf().  */
  if (totalsize == SIZE_MAX || totalsize > INT_MAX)
    {
      errno = EOVERFLOW;
      return NULL;
    }

  /* Allocate and fill the result string.  */
  result = (char *) xmalloc (totalsize + 1);
  p = result;
  for (i = argcount; i > 0; i--)
    {
      const char *next = va_arg (args, const char *);
      size_t len = strlen (next);
      memcpy (p, next, len);
      p += len;
    }
  *p = '\0';

  return result;
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]