libgfortran.h (GFC_ITOA_BUF_SIZE, [...]): Define.

* 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.

From-SVN: r104855
This commit is contained in:
Jakub Jelinek 2005-10-01 13:50:10 +02:00 committed by Jakub Jelinek
parent fdf84417b9
commit 1449b8cba8
7 changed files with 98 additions and 75 deletions

View File

@ -1,3 +1,24 @@
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.
2005-09-30 Janne Blomqvist <jblomqvi@cc.hut.fi>
PR 24112

View File

@ -621,7 +621,7 @@ read_f (fnode * f, char *dest, int length)
case '9':
case ' ':
ndigits++;
*p++;
p++;
w--;
break;

View File

@ -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;

View File

@ -29,6 +29,7 @@ the Free Software Foundation, 51 Franklin Street, Fifth Floor,
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 *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;
if (n == 0)
{
scratch[0] = '0';
scratch[1] = '\0';
return scratch;
}
assert (len >= GFC_OTOA_BUF_SIZE);
p = scratch + SCRATCH_SIZE - 1;
*p-- = '\0';
if (n == 0)
return "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;
if (n == 0)
{
scratch[0] = '0';
scratch[1] = '\0';
return scratch;
}
assert (len >= GFC_BTOA_BUF_SIZE);
p = scratch + SCRATCH_SIZE - 1;
*p-- = '\0';
if (n == 0)
return "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)
{

View File

@ -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));

View File

@ -28,6 +28,7 @@ the Free Software Foundation, 51 Franklin Street, Fifth Floor,
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;

View File

@ -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;
if (n == 0)
{
buffer[0] = '0';
buffer[1] = '\0';
return buffer;
}
assert (len >= GFC_XTOA_BUF_SIZE);
p = buffer + sizeof (buffer) - 1;
*p-- = '\0';
if (n == 0)
return "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);