Hi, the following legal program crashes with a segfault at runtime: program gfcbug60 implicit none integer :: i, j integer, allocatable :: mm(:) logical, allocatable :: mask(:) do i = 1, 0, -1 print *, "i =", j allocate (mm(i), mask(i)) mm(:) = 1 mask(:) = (mm == 0) j = count (mask) print *, "j =", j print *, "mm =", mm print *, "mask =", mask ! gfortran runtime crash for i=j=0: bug in pack for 0-sized arrays! print *, "pack (mm, mask) =", pack (mm, mask) deallocate (mm, mask) print * end do end program gfcbug60 % gfc gfcbug60.f90 && ./a.out i = 0 j = 0 mm = 1 mask = F pack (mm, mask) = i = 0 j = 0 mm = mask = Segmentation fault (core dumped)
Confirmed. I thought I had fixed it, though. Backtrace is (gdb) back #0 pack_internal (ret=0xbfab6628, array=Variable "array" is not available. ) at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/intrinsics/pack_generic.c:162 #1 0x08048c21 in MAIN__ () at pack.f90:18 #2 0x08048d78 in main (argc=Cannot access memory at address 0x4 ) at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/fmain.c:18 and I'll give it a look.
Patch below, currently regtesting. Index: libgfortran/intrinsics/pack_generic.c =================================================================== --- libgfortran/intrinsics/pack_generic.c (revision 122504) +++ libgfortran/intrinsics/pack_generic.c (working copy) @@ -93,15 +93,19 @@ index_type count[GFC_MAX_DIMENSIONS]; index_type extent[GFC_MAX_DIMENSIONS]; + int zero_sized; index_type n; index_type dim; index_type nelem; dim = GFC_DESCRIPTOR_RANK (array); + zero_sized = 0; for (n = 0; n < dim; n++) { count[n] = 0; extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound; + if (extent[n] <= 0) + zero_sized = 1; sstride[n] = array->dim[n].stride * size; mstride[n] = mask->dim[n].stride; } @@ -154,6 +158,8 @@ const GFC_LOGICAL_4 *m = mptr; total = 0; + if (zero_sized) + m = NULL; while (m) {
Subject: Bug 31001 Author: fxcoudert Date: Sat Mar 3 16:37:54 2007 New Revision: 122507 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=122507 Log: PR libfortran/31001 * intrinsics/pack_generic.c (pack_internal): Add special checks for zero-sized arrays. * gfortran.dg/zero_sized_3.f90: New test. Added: trunk/gcc/testsuite/gfortran.dg/zero_sized_3.f90 Modified: trunk/gcc/testsuite/ChangeLog trunk/libgfortran/ChangeLog trunk/libgfortran/intrinsics/pack_generic.c
Subject: Bug 31001 Author: fxcoudert Date: Thu Mar 8 12:34:59 2007 New Revision: 122697 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=122697 Log: PR target/30406 * config/rs6000/rs6000.c (rs6000_function_value): Look at bit size instead of precision. * gfortran.dg/logical_3.f90: New test. PR libfortran/31001 * intrinsics/pack_generic.c (pack_internal): Add special checks for zero-sized arrays. * gfortran.dg/zero_sized_3.f90: New test. Added: branches/gcc-4_2-branch/gcc/testsuite/gfortran.dg/logical_3.f90 - copied unchanged from r122523, trunk/gcc/testsuite/gfortran.dg/logical_3.f90 branches/gcc-4_2-branch/gcc/testsuite/gfortran.dg/zero_sized_3.f90 - copied unchanged from r122507, trunk/gcc/testsuite/gfortran.dg/zero_sized_3.f90 Modified: branches/gcc-4_2-branch/gcc/ChangeLog branches/gcc-4_2-branch/gcc/config/rs6000/rs6000.c branches/gcc-4_2-branch/gcc/testsuite/ChangeLog branches/gcc-4_2-branch/libgfortran/ChangeLog branches/gcc-4_2-branch/libgfortran/intrinsics/pack_generic.c
Fixed on 4.2 and 4.3.