Patch for libgfortran PR 18891
Steve Ellcey
sje@cup.hp.com
Mon Dec 13 17:16:00 GMT 2004
> > ! memcpy (p, &zero, sizeof (gfc_offset)); /* Bogus value for now. */
>
> Better to use memset.
>
> r~
I don't know why that never occured to me. I will change it.
> > I also changed some of my memcpy calls to use an
> > existing variable as the length of the memcpy instead of sizeof()
> > because the variable in question was set based on sizeof() earlier in
> > the routine.
>
> I don't buy this argument.
>
> > + gfc_offset i;
> ...
> > ! memcpy (&i, p, n);
> > ! current_unit->bytes_left = i;
>
> n had better be sizeof(i), or you are reading from uninitialized memory.
>
> r~
I made sure the variables in question are always set to the right value
and they are because the libgfortran alloc routine (salloc_r & salloc_w)
require a pointer to a size so these variables are set to the sizeof
value for that use. But I agree that it really isn't better to use the
variables instead of sizeof and I am not sure why I thought it was so I
switched it back.
I retested with no regressions and here is the updated patch:
OK for checkin?
Steve Ellcey
sje@cup.hp.com
2004-12-13 Steve Ellcey <sje@cup.hp.com>
* libgfortran/io/transfer.c (us_read): Use memcpy/memset
instead of assignment to fill unaligned buffer.
(us_write): Ditto.
(next_record_w): Ditto.
*** gcc.orig/libgfortran/io/transfer.c Fri Dec 10 08:57:51 2004
--- gcc/libgfortran/io/transfer.c Mon Dec 13 09:08:27 2004
*************** transfer_complex (void *p, int kind)
*** 819,829 ****
static void
us_read (void)
{
! gfc_offset *p;
int n;
n = sizeof (gfc_offset);
! p = (gfc_offset *) salloc_r (current_unit->s, &n);
if (p == NULL || n != sizeof (gfc_offset))
{
--- 819,830 ----
static void
us_read (void)
{
! char *p;
int n;
+ gfc_offset i;
n = sizeof (gfc_offset);
! p = salloc_r (current_unit->s, &n);
if (p == NULL || n != sizeof (gfc_offset))
{
*************** us_read (void)
*** 831,837 ****
return;
}
! current_unit->bytes_left = *p;
}
--- 832,839 ----
return;
}
! memcpy (&i, p, sizeof (gfc_offset));
! current_unit->bytes_left = i;
}
*************** us_read (void)
*** 841,851 ****
static void
us_write (void)
{
! gfc_offset *p;
int length;
length = sizeof (gfc_offset);
! p = (gfc_offset *) salloc_w (current_unit->s, &length);
if (p == NULL)
{
--- 843,853 ----
static void
us_write (void)
{
! char *p;
int length;
length = sizeof (gfc_offset);
! p = salloc_w (current_unit->s, &length);
if (p == NULL)
{
*************** us_write (void)
*** 853,859 ****
return;
}
! *p = 0; /* Bogus value for now. */
if (sfree (current_unit->s) == FAILURE)
generate_error (ERROR_OS, NULL);
--- 855,861 ----
return;
}
! memset (p, '\0', sizeof (gfc_offset)); /* Bogus value for now. */
if (sfree (current_unit->s) == FAILURE)
generate_error (ERROR_OS, NULL);
*************** next_record_w (int done)
*** 1281,1287 ****
if (p == NULL)
goto io_error;
! *((gfc_offset *) p) = m;
if (sfree (current_unit->s) == FAILURE)
goto io_error;
--- 1283,1289 ----
if (p == NULL)
goto io_error;
! memcpy (p, &m, sizeof (gfc_offset));
if (sfree (current_unit->s) == FAILURE)
goto io_error;
*************** next_record_w (int done)
*** 1292,1298 ****
if (p == NULL)
generate_error (ERROR_OS, NULL);
! *((gfc_offset *) p) = m;
if (sfree (current_unit->s) == FAILURE)
goto io_error;
--- 1294,1300 ----
if (p == NULL)
generate_error (ERROR_OS, NULL);
! memcpy (p, &m, sizeof (gfc_offset));
if (sfree (current_unit->s) == FAILURE)
goto io_error;
More information about the Fortran
mailing list