bug-gnulib
[Top][All Lists]
Advanced

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

vasnprintf: tweak size computations


From: Bruno Haible
Subject: vasnprintf: tweak size computations
Date: Sun, 10 Jun 2007 13:58:13 +0200
User-agent: KMail/1.5.4

In vasnprintf, when USE_SNPRINTF is not defined, one doesn't need a local
variable 'maxlen'. Also, the memory allocation was a little too conservative,
wasting 1 byte.

2007-06-10  Bruno Haible  <address@hidden>

        * lib/vasnprintf.c (VASNPRINTF) [!USE_SNPRINTF]: Remove variable
        'maxlen'. Ensure only length + width bytes are allocated, not
        length + 1 + width.

*** vasnprintf.c        10 Jun 2007 11:18:49 -0000      1.56
--- vasnprintf.c        10 Jun 2007 11:49:57 -0000
***************
*** 2812,2826 ****
  
                for (;;)
                  {
!                   size_t maxlen;
!                   int count;
!                   int retcount;
! 
!                   maxlen = allocated - length;
!                   count = -1;
!                   retcount = 0;
  
  #if USE_SNPRINTF
                    /* SNPRINTF can fail if maxlen > INT_MAX.  */
                    if (maxlen > INT_MAX)
                      goto overflow;
--- 2812,2822 ----
  
                for (;;)
                  {
!                   int count = -1;
!                   int retcount = 0;
  
  #if USE_SNPRINTF
+                   size_t maxlen = allocated - length;
                    /* SNPRINTF can fail if maxlen > INT_MAX.  */
                    if (maxlen > INT_MAX)
                      goto overflow;
***************
*** 3035,3040 ****
--- 3031,3037 ----
                        return NULL;
                      }
  
+ #if USE_SNPRINTF
                    /* Make room for the result.  */
                    if (count >= maxlen)
                      {
***************
*** 3045,3056 ****
                          xmax (xsum (length, count), xtimes (allocated, 2));
  
                        ENSURE_ALLOCATION (n);
- #if USE_SNPRINTF
                        continue;
! #else
!                       maxlen = allocated - length;
  #endif
                      }
  
                    /* Perform padding.  */
  #if NEED_PRINTF_FLAG_ZERO
--- 3042,3065 ----
                          xmax (xsum (length, count), xtimes (allocated, 2));
  
                        ENSURE_ALLOCATION (n);
                        continue;
!                     }
  #endif
+ 
+ #if !USE_SNPRINTF
+                   /* Make room for the result.  */
+                   if (count > allocated - length)
+                     {
+                       /* Need at least count bytes.  But allocate
+                          proportionally.  */
+                       size_t n =
+                         xmax (xsum (length, count), xtimes (allocated, 2));
+ 
+                       ENSURE_ALLOCATION (n);
                      }
+ #endif
+ 
+                   /* Here count <= allocated - length.  */
  
                    /* Perform padding.  */
  #if NEED_PRINTF_FLAG_ZERO
***************
*** 3058,3078 ****
                      {
  # if USE_SNPRINTF
                        /* Make room for the result.  */
!                       if (width >= maxlen)
                          {
                            /* Need at least width bytes.  But allocate
                               proportionally, to avoid looping eternally if
                               snprintf() reports a too small count.  */
                            size_t n =
!                             xmax (xsum (length + 1, width),
                                    xtimes (allocated, 2));
  
                            length += count;
                            ENSURE_ALLOCATION (n);
                            length -= count;
-                           maxlen = allocated - length; /* > width */
                          }
!                       /* Here width < maxlen.  */
  # endif
                        {
  # if USE_SNPRINTF
--- 3067,3086 ----
                      {
  # if USE_SNPRINTF
                        /* Make room for the result.  */
!                       if (width > maxlen)
                          {
                            /* Need at least width bytes.  But allocate
                               proportionally, to avoid looping eternally if
                               snprintf() reports a too small count.  */
                            size_t n =
!                             xmax (xsum (length, width),
                                    xtimes (allocated, 2));
  
                            length += count;
                            ENSURE_ALLOCATION (n);
                            length -= count;
                          }
!                       /* Here width <= allocated - length.  */
  # endif
                        {
  # if USE_SNPRINTF
***************
*** 3131,3137 ****
                      abort ();
  #endif
  
!                   /* Here still count < maxlen.  */
  
  #if USE_SNPRINTF
                    /* The snprintf() result did fit.  */
--- 3139,3145 ----
                      abort ();
  #endif
  
!                   /* Here still count <= allocated - length.  */
  
  #if USE_SNPRINTF
                    /* The snprintf() result did fit.  */





reply via email to

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