This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] stdio-common/vfprintf.c: Remove magic number 32.


I was looking at modifying some of this code for a bug I was working
on and it was super annoying that the 32 character buffer is hardcoded
everywhere. This patch simply hoists this into a macro EXTSIZ.

I'll check this in within 24h if nobody objects.

2017-05-04  Carlos O'Donell  <carlos@redhat.com>

	* stdio-common/vfprintf.c (EXTSIZ): Define.
	(vfprintf): Use EXTSIZ.
	(printf_positional): Likewise.

diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
index 54b1ba2..2cf7c8a 100644
--- a/stdio-common/vfprintf.c
+++ b/stdio-common/vfprintf.c
@@ -45,6 +45,10 @@
 #define va_list	_IO_va_list
 #undef BUFSIZ
 #define BUFSIZ		_IO_BUFSIZ
+/* In some cases we need extra space for all the output which is not
+   counted in the width of the string. We assume 32 characters is
+   enough.  */
+#define EXTSIZ		32
 #define ARGCHECK(S, Format) \
   do									      \
     {									      \
@@ -1456,20 +1460,19 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
 	    left = 1;
 	  }
 
-	if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - 32))
+	if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
 	  {
 	    __set_errno (EOVERFLOW);
 	    done = -1;
 	    goto all_done;
 	  }
 
-	if (width >= WORK_BUFFER_SIZE - 32)
+	if (width >= WORK_BUFFER_SIZE - EXTSIZ)
 	  {
-	    /* We have to use a special buffer.  The "32" is just a safe
-	       bet for all the output which is not counted in the width.  */
-	    size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
+	    /* We have to use a special buffer.  */
+	    size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
 	    if (__libc_use_alloca (needed))
-	      workend = (CHAR_T *) alloca (needed) + width + 32;
+	      workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
 	    else
 	      {
 		workstart = (CHAR_T *) malloc (needed);
@@ -1478,7 +1481,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
 		    done = -1;
 		    goto all_done;
 		  }
-		workend = workstart + width + 32;
+		workend = workstart + width + EXTSIZ;
 	      }
 	  }
       }
@@ -1489,20 +1492,19 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
       width = read_int (&f);
 
       if (__glibc_unlikely (width == -1
-			    || width >= INT_MAX / sizeof (CHAR_T) - 32))
+			    || width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
 	{
 	  __set_errno (EOVERFLOW);
 	  done = -1;
 	  goto all_done;
 	}
 
-      if (width >= WORK_BUFFER_SIZE - 32)
+      if (width >= WORK_BUFFER_SIZE - EXTSIZ)
 	{
-	  /* We have to use a special buffer.  The "32" is just a safe
-	     bet for all the output which is not counted in the width.  */
-	  size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
+	  /* We have to use a special buffer.  */
+	  size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
 	  if (__libc_use_alloca (needed))
-	    workend = (CHAR_T *) alloca (needed) + width + 32;
+	    workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
 	  else
 	    {
 	      workstart = (CHAR_T *) malloc (needed);
@@ -1511,7 +1513,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
 		  done = -1;
 		  goto all_done;
 		}
-	      workend = workstart + width + 32;
+	      workend = workstart + width + EXTSIZ;
 	    }
 	}
       if (*f == L_('$'))
@@ -1562,23 +1564,23 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
 	}
       else
 	prec = 0;
-      if (prec > width && prec > WORK_BUFFER_SIZE - 32)
+      if (prec > width && prec > WORK_BUFFER_SIZE - EXTSIZ)
 	{
 	  /* Deallocate any previously allocated buffer because it is
 	     too small.  */
 	  if (__glibc_unlikely (workstart != NULL))
 	    free (workstart);
 	  workstart = NULL;
-	  if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - 32))
+	  if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
 	    {
 	      __set_errno (EOVERFLOW);
 	      done = -1;
 	      goto all_done;
 	    }
-	  size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
+	  size_t needed = ((size_t) prec + EXTSIZ) * sizeof (CHAR_T);
 
 	  if (__libc_use_alloca (needed))
-	    workend = (CHAR_T *) alloca (needed) + prec + 32;
+	    workend = (CHAR_T *) alloca (needed) + prec + EXTSIZ;
 	  else
 	    {
 	      workstart = (CHAR_T *) malloc (needed);
@@ -1587,7 +1589,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
 		  done = -1;
 		  goto all_done;
 		}
-	      workend = workstart + prec + 32;
+	      workend = workstart + prec + EXTSIZ;
 	    }
 	}
       JUMP (*f, step2_jumps);
@@ -1964,23 +1966,23 @@ printf_positional (_IO_FILE *s, const CHAR_T *format, int readonly_format,
 	}
 
       /* Maybe the buffer is too small.  */
-      if (MAX (prec, width) + 32 > WORK_BUFFER_SIZE)
+      if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
 	{
-	  if (__libc_use_alloca ((MAX (prec, width) + 32)
+	  if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
 				 * sizeof (CHAR_T)))
-	    workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
+	    workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
 					  * sizeof (CHAR_T))
-		       + (MAX (prec, width) + 32));
+		       + (MAX (prec, width) + EXTSIZ));
 	  else
 	    {
-	      workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
+	      workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
 					     * sizeof (CHAR_T));
 	      if (workstart == NULL)
 		{
 		  done = -1;
 		  goto all_done;
 		}
-	      workend = workstart + (MAX (prec, width) + 32);
+	      workend = workstart + (MAX (prec, width) + EXTSIZ);
 	    }
 	}
---

-- 
Cheers,
Carlos.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]