From 3d894fc3f045e430b67aae82aa68dd818816ac90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Coudert?= Date: Thu, 19 Oct 2006 21:48:50 +0000 Subject: re PR libfortran/27895 (problem with RESHAPE and zero-sized arrays) PR libfortran/27895 * intrinsics/cshift0.c: Special cases for zero-sized arrays. * intrinsics/pack_generic.c: Likewise. * intrinsics/spread_generic.c: Likewise. * gfortran.dg/zero_sized_1.f90: New test. From-SVN: r117890 --- libgfortran/intrinsics/cshift0.c | 15 +++++++++--- libgfortran/intrinsics/pack_generic.c | 42 ++++++++++++++++++--------------- libgfortran/intrinsics/spread_generic.c | 8 ++++++- 3 files changed, 42 insertions(+), 23 deletions(-) (limited to 'libgfortran/intrinsics') diff --git a/libgfortran/intrinsics/cshift0.c b/libgfortran/intrinsics/cshift0.c index 4df90ad..f2c2219 100644 --- a/libgfortran/intrinsics/cshift0.c +++ b/libgfortran/intrinsics/cshift0.c @@ -1,5 +1,5 @@ /* Generic implementation of the CSHIFT intrinsic - Copyright 2003, 2005 Free Software Foundation, Inc. + Copyright 2003, 2005, 2006 Free Software Foundation, Inc. Contributed by Feng Wang This file is part of the GNU Fortran 95 runtime library (libgfortran). @@ -144,8 +144,8 @@ cshift0 (gfc_array_char * ret, const gfc_array_char * array, if (ret->data == NULL) { int i; + index_type arraysize = size0 ((array_t *)array); - ret->data = internal_malloc_size (size * size0 ((array_t *)array)); ret->offset = 0; ret->dtype = array->dtype; for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++) @@ -156,8 +156,17 @@ cshift0 (gfc_array_char * ret, const gfc_array_char * array, if (i == 0) ret->dim[i].stride = 1; else - ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride; + ret->dim[i].stride = (ret->dim[i-1].ubound + 1) + * ret->dim[i-1].stride; } + + if (arraysize > 0) + ret->data = internal_malloc_size (size * arraysize); + else + { + ret->data = internal_malloc_size (1); + return; + } } for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++) diff --git a/libgfortran/intrinsics/pack_generic.c b/libgfortran/intrinsics/pack_generic.c index 27a22ec..1b0d725 100644 --- a/libgfortran/intrinsics/pack_generic.c +++ b/libgfortran/intrinsics/pack_generic.c @@ -1,5 +1,5 @@ /* Generic implementation of the PACK intrinsic - Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. Contributed by Paul Brook This file is part of the GNU Fortran 95 runtime library (libgfortran). @@ -195,12 +195,15 @@ pack_internal (gfc_array_char *ret, const gfc_array_char *array, ret->dim[0].ubound = total - 1; ret->dim[0].stride = 1; - ret->data = internal_malloc_size (size * total); ret->offset = 0; - if (total == 0) - /* In this case, nothing remains to be done. */ - return; + { + /* In this case, nothing remains to be done. */ + ret->data = internal_malloc_size (1); + return; + } + else + ret->data = internal_malloc_size (size * total); } rstride0 = ret->dim[0].stride * size; @@ -210,7 +213,7 @@ pack_internal (gfc_array_char *ret, const gfc_array_char *array, mstride0 = mstride[0]; rptr = ret->data; - while (sptr) + while (sptr && mptr) { /* Test this element. */ if (*mptr) @@ -315,14 +318,17 @@ pack_s_internal (gfc_array_char *ret, const gfc_array_char *array, index_type extent[GFC_MAX_DIMENSIONS]; index_type n; index_type dim; + index_type ssize; index_type nelem; dim = GFC_DESCRIPTOR_RANK (array); + ssize = 1; for (n = 0; n < dim; n++) { count[n] = 0; extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound; sstride[n] = array->dim[n].stride * size; + ssize *= extent[n]; } if (sstride[0] == 0) sstride[0] = size; @@ -352,25 +358,23 @@ pack_s_internal (gfc_array_char *ret, const gfc_array_char *array, total *= extent[n]; } else - { - /* The result array will be empty. */ - ret->dim[0].lbound = 0; - ret->dim[0].ubound = -1; - ret->dim[0].stride = 1; - ret->data = internal_malloc_size (0); - ret->offset = 0; - - return; - } + /* The result array will be empty. */ + total = 0; } /* Setup the array descriptor. */ ret->dim[0].lbound = 0; ret->dim[0].ubound = total - 1; ret->dim[0].stride = 1; - - ret->data = internal_malloc_size (size * total); ret->offset = 0; + + if (total == 0) + { + ret->data = internal_malloc_size (1); + return; + } + else + ret->data = internal_malloc_size (size * total); } rstride0 = ret->dim[0].stride * size; @@ -384,7 +388,7 @@ pack_s_internal (gfc_array_char *ret, const gfc_array_char *array, If MASK is .FALSE., we have to copy VECTOR into the result array. If VECTOR were not present we would have already returned. */ - if (*mask) + if (*mask && ssize != 0) { while (sptr) { diff --git a/libgfortran/intrinsics/spread_generic.c b/libgfortran/intrinsics/spread_generic.c index cbc5c49..9ea6b12 100644 --- a/libgfortran/intrinsics/spread_generic.c +++ b/libgfortran/intrinsics/spread_generic.c @@ -101,7 +101,13 @@ spread_internal (gfc_array_char *ret, const gfc_array_char *source, } } ret->offset = 0; - ret->data = internal_malloc_size (rs * size); + if (rs > 0) + ret->data = internal_malloc_size (rs * size); + else + { + ret->data = internal_malloc_size (1); + return; + } } else { -- cgit v1.1