Bug 31001 - [4.1 only] PACK crashes on zero-sized arrays
Summary: [4.1 only] PACK crashes on zero-sized arrays
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.3.0
: P5 normal
Target Milestone: 4.2.0
Assignee: Francois-Xavier Coudert
URL:
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2007-02-28 22:16 UTC by Harald Anlauf
Modified: 2007-03-14 10:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.3.0 4.2.0
Known to fail: 4.1.2
Last reconfirmed: 2007-03-04 07:20:24


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Harald Anlauf 2007-02-28 22:16:25 UTC
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)
Comment 1 Francois-Xavier Coudert 2007-03-03 10:38:13 UTC
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.
Comment 2 Francois-Xavier Coudert 2007-03-03 14:24:04 UTC
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)
            {
Comment 3 Francois-Xavier Coudert 2007-03-03 16:38:07 UTC
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

Comment 4 Francois-Xavier Coudert 2007-03-08 12:35:16 UTC
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

Comment 5 Francois-Xavier Coudert 2007-03-14 10:49:25 UTC
Fixed on 4.2 and 4.3.