This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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] Avoid using static buffers in {gfc_,}[ixob]toa


Hi!

One of the easy (and tiny) steps toward libgfortran thread safety.
Rather than using static buffers, callers create a temporary buffer
on their stack and pass them down to the functions.

In addition to this, I have fixed 2 warnings.

Tested on x86_64-linux.
Ok for HEAD?

2005-10-01  Jakub Jelinek  <jakub@redhat.com>

	* libgfortran.h (GFC_ITOA_BUF_SIZE, GFC_XTOA_BUF_SIZE,
	GFC_OTOA_BUF_SIZE, GFC_BTOA_BUF_SIZE): Define.
	(gfc_itoa, xtoa): Add 2 extra arguments.
	* runtime/environ.c: Include stdio.h.
	(check_buffered): Use sprintf.
	* runtime/error.c: Include assert.h.
	(gfc_itoa, xtoa): Add 2 extra arguments, avoid using static
	buffers.
	(st_printf, st_sprintf): Adjust callers.
	* io/write.c (otoa, btoa): Add 2 extra arguments, avoid using
	static buffers.
	(write_int, write_decimal): Add 2 extra arguments to conv
	function pointer, adjust caller.
	(write_integer): Adjust gfc_itoa caller.

	* io/unit.c (get_array_unit_len): Return 0 rather than NULL.

	* io/read.c (read_f): Remove spurious pointer dereference.
	
--- libgfortran/libgfortran.h.jj	2005-09-29 15:52:20.000000000 +0200
+++ libgfortran/libgfortran.h	2005-10-01 10:50:10.000000000 +0200
@@ -413,10 +413,15 @@ internal_proto(get_args);
 
 /* error.c */
 
-extern char *gfc_itoa (GFC_INTEGER_LARGEST);
+#define GFC_ITOA_BUF_SIZE (sizeof (GFC_INTEGER_LARGEST) * 3 + 2)
+#define GFC_XTOA_BUF_SIZE (sizeof (GFC_UINTEGER_LARGEST) * 2 + 1)
+#define GFC_OTOA_BUF_SIZE (sizeof (GFC_INTEGER_LARGEST) * 3 + 1)
+#define GFC_BTOA_BUF_SIZE (sizeof (GFC_INTEGER_LARGEST) * 8 + 1)
+
+extern const char *gfc_itoa (GFC_INTEGER_LARGEST, char *, size_t);
 internal_proto(gfc_itoa);
 
-extern char *xtoa (GFC_UINTEGER_LARGEST);
+extern const char *xtoa (GFC_UINTEGER_LARGEST, char *, size_t);
 internal_proto(xtoa);
 
 extern void os_error (const char *) __attribute__ ((noreturn));
--- libgfortran/runtime/environ.c.jj	2005-09-29 13:50:11.000000000 +0200
+++ libgfortran/runtime/environ.c	2005-10-01 10:57:56.000000000 +0200
@@ -28,6 +28,7 @@ the Free Software Foundation, 51 Frankli
 Boston, MA 02110-1301, USA.  */
 
 #include "config.h"
+#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
@@ -580,15 +581,14 @@ init_variables (void)
 int
 check_buffered (int n)
 {
-  char name[40];
+  char name[22 + sizeof (n) * 3];
   variable v;
   int rv;
 
   if (options.all_unbuffered)
     return 0;
 
-  strcpy (name, "GFORTRAN_UNBUFFERED_");
-  strcat (name, gfc_itoa (n));
+  sprintf (name, "GFORTRAN_UNBUFFERED_%d", n);
 
   v.name = name;
   v.value = 2;
--- libgfortran/runtime/error.c.jj	2005-09-20 11:06:23.000000000 +0200
+++ libgfortran/runtime/error.c	2005-09-30 23:44:33.000000000 +0200
@@ -29,6 +29,7 @@ Boston, MA 02110-1301, USA.  */
 
 
 #include "config.h"
+#include <assert.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
@@ -63,25 +64,19 @@ iexport_data(filename);
 unsigned line = 0;
 iexport_data(line);
 
-/* buffer for integer/ascii conversions.  */
-static char buffer[sizeof (GFC_UINTEGER_LARGEST) * 8 + 1];
+/* gfc_itoa()-- Integer to decimal conversion. */
 
-
-/* Returns a pointer to a static buffer. */
-
-char *
-gfc_itoa (GFC_INTEGER_LARGEST n)
+const char *
+gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
 {
   int negative;
   char *p;
   GFC_UINTEGER_LARGEST t;
 
+  assert (len >= GFC_ITOA_BUF_SIZE);
+
   if (n == 0)
-    {
-      buffer[0] = '0';
-      buffer[1] = '\0';
-      return buffer;
-    }
+    return "0";
 
   negative = 0;
   t = n;
@@ -91,39 +86,36 @@ gfc_itoa (GFC_INTEGER_LARGEST n)
       t = -n; /*must use unsigned to protect from overflow*/
     }
 
-  p = buffer + sizeof (buffer) - 1;
-  *p-- = '\0';
+  p = buffer + GFC_ITOA_BUF_SIZE - 1;
+  *p = '\0';
 
   while (t != 0)
     {
-      *p-- = '0' + (t % 10);
+      *--p = '0' + (t % 10);
       t /= 10;
     }
 
   if (negative)
-    *p-- = '-';
-  return ++p;
+    *--p = '-';
+  return p;
 }
 
 
-/* xtoa()-- Integer to hexadecimal conversion.  Returns a pointer to a
- * static buffer. */
+/* xtoa()-- Integer to hexadecimal conversion.  */
 
-char *
-xtoa (GFC_UINTEGER_LARGEST n)
+const char *
+xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
 {
   int digit;
   char *p;
 
+  assert (len >= GFC_XTOA_BUF_SIZE);
+
   if (n == 0)
-    {
-      buffer[0] = '0';
-      buffer[1] = '\0';
-      return buffer;
-    }
+    return "0";
 
-  p = buffer + sizeof (buffer) - 1;
-  *p-- = '\0';
+  p = buffer + GFC_XTOA_BUF_SIZE - 1;
+  *p = '\0';
 
   while (n != 0)
     {
@@ -131,11 +123,11 @@ xtoa (GFC_UINTEGER_LARGEST n)
       if (digit > 9)
 	digit += 'A' - '0' - 10;
 
-      *p-- = '0' + digit;
+      *--p = '0' + digit;
       n >>= 4;
     }
 
-  return ++p;
+  return p;
 }
 
 
@@ -149,8 +141,10 @@ st_printf (const char *format, ...)
 {
   int count, total;
   va_list arg;
-  char *p, *q;
+  char *p;
+  const char *q;
   stream *s;
+  char itoa_buf[GFC_ITOA_BUF_SIZE];
 
   total = 0;
   s = init_error_stream ();
@@ -187,7 +181,7 @@ st_printf (const char *format, ...)
 	  break;
 
 	case 'd':
-	  q = gfc_itoa (va_arg (arg, int));
+	  q = gfc_itoa (va_arg (arg, int), itoa_buf, sizeof (itoa_buf));
 	  count = strlen (q);
 
 	  p = salloc_w (s, &count);
@@ -196,7 +190,7 @@ st_printf (const char *format, ...)
 	  break;
 
 	case 'x':
-	  q = xtoa (va_arg (arg, unsigned));
+	  q = xtoa (va_arg (arg, unsigned), itoa_buf, sizeof (itoa_buf));
 	  count = strlen (q);
 
 	  p = salloc_w (s, &count);
@@ -240,8 +234,10 @@ void
 st_sprintf (char *buffer, const char *format, ...)
 {
   va_list arg;
-  char c, *p;
+  char c;
+  const char *p;
   int count;
+  char itoa_buf[GFC_ITOA_BUF_SIZE];
 
   va_start (arg, format);
 
@@ -264,7 +260,7 @@ st_sprintf (char *buffer, const char *fo
 	  break;
 
 	case 'd':
-	  p = gfc_itoa (va_arg (arg, int));
+	  p = gfc_itoa (va_arg (arg, int), itoa_buf, sizeof (itoa_buf));
 	  count = strlen (p);
 
 	  memcpy (buffer, p, count);
--- libgfortran/io/unit.c.jj	2005-09-20 11:06:23.000000000 +0200
+++ libgfortran/io/unit.c	2005-10-01 10:58:48.000000000 +0200
@@ -261,7 +261,7 @@ get_array_unit_len (gfc_array_char *desc
       if (desc->dim[i].stride != stride)
 	{
 	  generate_error (ERROR_ARRAY_STRIDE, NULL);
-	  return NULL;
+	  return 0;
 	}
       stride *= desc->dim[i].ubound;
       record_count *= desc->dim[i].ubound;
--- libgfortran/io/read.c.jj	2005-10-01 11:01:57.000000000 +0200
+++ libgfortran/io/read.c	2005-10-01 11:01:57.000000000 +0200
@@ -621,7 +621,7 @@ read_f (fnode * f, char *dest, int lengt
 	case '9':
 	case ' ':
 	  ndigits++;
-	  *p++;
+	  p++;
 	  w--;
 	  break;
 
--- libgfortran/io/write.c.jj	2005-09-29 15:52:21.000000000 +0200
+++ libgfortran/io/write.c	2005-09-30 23:47:43.000000000 +0200
@@ -29,6 +29,7 @@ the Free Software Foundation, 51 Frankli
 Boston, MA 02110-1301, USA.  */
 
 #include "config.h"
+#include <assert.h>
 #include <string.h>
 #include <ctype.h>
 #include <float.h>
@@ -910,11 +911,13 @@ write_float (fnode *f, const char *sourc
 
 static void
 write_int (fnode *f, const char *source, int len,
-           char *(*conv) (GFC_UINTEGER_LARGEST))
+           const char *(*conv) (GFC_UINTEGER_LARGEST, char *, size_t))
 {
   GFC_UINTEGER_LARGEST n = 0;
   int w, m, digits, nzero, nblank;
-  char *p, *q;
+  char *p;
+  const char *q;
+  char itoa_buf[GFC_BTOA_BUF_SIZE];
 
   w = f->u.integer.w;
   m = f->u.integer.m;
@@ -936,7 +939,7 @@ write_int (fnode *f, const char *source,
       goto done;
     }
 
-  q = conv (n);
+  q = conv (n, itoa_buf, sizeof (itoa_buf));
   digits = strlen (q);
 
   /* Select a width if none was specified.  The idea here is to always
@@ -988,12 +991,14 @@ write_int (fnode *f, const char *source,
 
 static void
 write_decimal (fnode *f, const char *source, int len,
-               char *(*conv) (GFC_INTEGER_LARGEST))
+               const char *(*conv) (GFC_INTEGER_LARGEST, char *, size_t))
 {
   GFC_INTEGER_LARGEST n = 0;
   int w, m, digits, nsign, nzero, nblank;
-  char *p, *q;
+  char *p;
+  const char *q;
   sign_t sign;
+  char itoa_buf[GFC_BTOA_BUF_SIZE];
 
   w = f->u.integer.w;
   m = f->u.integer.m;
@@ -1020,7 +1025,7 @@ write_decimal (fnode *f, const char *sou
     n = -n;
 
   nsign = sign == SIGN_NONE ? 0 : 1;
-  q = conv (n);
+  q = conv (n, itoa_buf, sizeof (itoa_buf));
 
   digits = strlen (q);
 
@@ -1075,56 +1080,51 @@ write_decimal (fnode *f, const char *sou
 
 /* Convert unsigned octal to ascii.  */
 
-static char *
-otoa (GFC_UINTEGER_LARGEST n)
+static const char *
+otoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
 {
   char *p;
 
+  assert (len >= GFC_OTOA_BUF_SIZE);
+
   if (n == 0)
-    {
-      scratch[0] = '0';
-      scratch[1] = '\0';
-      return scratch;
-    }
+    return "0";
 
-  p = scratch + SCRATCH_SIZE - 1;
-  *p-- = '\0';
+  p = buffer + GFC_OTOA_BUF_SIZE - 1;
+  *p = '\0';
 
   while (n != 0)
     {
-      *p = '0' + (n & 7);
-      p--;
+      *--p = '0' + (n & 7);
       n >>= 3;
     }
 
-  return ++p;
+  return p;
 }
 
 
 /* Convert unsigned binary to ascii.  */
 
-static char *
-btoa (GFC_UINTEGER_LARGEST n)
+static const char *
+btoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
 {
   char *p;
 
+  assert (len >= GFC_BTOA_BUF_SIZE);
+
   if (n == 0)
-    {
-      scratch[0] = '0';
-      scratch[1] = '\0';
-      return scratch;
-    }
+    return "0";
 
-  p = scratch + SCRATCH_SIZE - 1;
-  *p-- = '\0';
+  p = buffer + GFC_BTOA_BUF_SIZE - 1;
+  *p = '\0';
 
   while (n != 0)
     {
-      *p-- = '0' + (n & 1);
+      *--p = '0' + (n & 1);
       n >>= 1;
     }
 
-  return ++p;
+  return p;
 }
 
 
@@ -1245,8 +1245,9 @@ write_integer (const char *source, int l
   const char *q;
   int digits;
   int width;
+  char itoa_buf[GFC_ITOA_BUF_SIZE];
 
-  q = gfc_itoa (extract_int (source, length));
+  q = gfc_itoa (extract_int (source, length), itoa_buf, sizeof (itoa_buf));
 
   switch (length)
     {

	Jakub


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