From 556d02696528fbe7eacce1db1fab1e8bb7e6b736 Mon Sep 17 00:00:00 2001 From: jakub Date: Sat, 1 Oct 2005 11:50:10 +0000 Subject: [PATCH] * 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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@104855 138bc75d-0d04-0410-961f-82ee72b054a4 --- libgfortran/ChangeLog | 21 ++++++++++++++ libgfortran/io/read.c | 2 +- libgfortran/io/unit.c | 2 +- libgfortran/io/write.c | 61 +++++++++++++++++++-------------------- libgfortran/libgfortran.h | 9 ++++-- libgfortran/runtime/environ.c | 6 ++-- libgfortran/runtime/error.c | 66 ++++++++++++++++++++----------------------- 7 files changed, 95 insertions(+), 72 deletions(-) diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog index 07b2e91..3b5abd2 100644 --- a/libgfortran/ChangeLog +++ b/libgfortran/ChangeLog @@ -1,3 +1,24 @@ +2005-10-01 Jakub Jelinek + + * 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. + 2005-09-30 Janne Blomqvist PR 24112 diff --git a/libgfortran/io/read.c b/libgfortran/io/read.c index ec6077c..a3a221a 100644 --- a/libgfortran/io/read.c +++ b/libgfortran/io/read.c @@ -621,7 +621,7 @@ read_f (fnode * f, char *dest, int length) case '9': case ' ': ndigits++; - *p++; + p++; w--; break; diff --git a/libgfortran/io/unit.c b/libgfortran/io/unit.c index 586e9ed..b078d87 100644 --- a/libgfortran/io/unit.c +++ b/libgfortran/io/unit.c @@ -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; diff --git a/libgfortran/io/write.c b/libgfortran/io/write.c index c1bf78e..b21399f 100644 --- a/libgfortran/io/write.c +++ b/libgfortran/io/write.c @@ -29,6 +29,7 @@ the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "config.h" +#include #include #include #include @@ -910,11 +911,13 @@ write_float (fnode *f, const char *source, int len) 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, int len, 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, int len, 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 *source, int len, 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 *source, int len, /* 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 length) 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) { diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h index ba8261d..49d2c619 100644 --- a/libgfortran/libgfortran.h +++ b/libgfortran/libgfortran.h @@ -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)); diff --git a/libgfortran/runtime/environ.c b/libgfortran/runtime/environ.c index e86f2ce..fb5da20 100644 --- a/libgfortran/runtime/environ.c +++ b/libgfortran/runtime/environ.c @@ -28,6 +28,7 @@ the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "config.h" +#include #include #include #include @@ -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; diff --git a/libgfortran/runtime/error.c b/libgfortran/runtime/error.c index 7c708e3..60fc56c 100644 --- a/libgfortran/runtime/error.c +++ b/libgfortran/runtime/error.c @@ -29,6 +29,7 @@ Boston, MA 02110-1301, USA. */ #include "config.h" +#include #include #include #include @@ -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 *format, ...) 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); -- 2.7.4