http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52151
Bug #: 52151 Summary: Segfault with realloc on assignment and RESHAPE to unallocated LHS Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: fortran AssignedTo: unassig...@gcc.gnu.org ReportedBy: bur...@gcc.gnu.org CC: pa...@gcc.gnu.org Follow up to PR 52012 and 52117. * B is allocated with the shape (n1,n2,n3): -> Works since the patch for PR 52012 (Before, it had the wrong result for the last two elements) * B is not allocated -> Segfault since the patch for PR 52012 -> Before: no segfault but wrong result in for the last two elements * B allocated with shape (1,1,1) -> Since patch for PR 52012: No crash, but wrong result ("2.0" and 3.0 appear multiple times, 5., 6., 7., 8. are missing and 4. is at the wrong position) -> Before: no segfault but wrong result in for the last two elements The test case has been taken from PR 52117 with adaption to show this bug. chapter08/puppeteer_f2003 of Damian's/Xia's/Xu's book is also affected. I think there might be an ordering issue with regards to the zero condition and the bounds condition. Something like the following might work (completely untested; note also the "ORIF"): --- trans-expr.c (revision 183971) +++ trans-expr.c (working copy) @@ -6296 +6296 @@ fcncall_realloc_result (gfc_se *se, int - tree zero_cond; + tree zero_cond, cond; @@ -6324,0 +6325 @@ fcncall_realloc_result (gfc_se *se, int + cond = boolean_true_node; @@ -6344 +6345 @@ fcncall_realloc_result (gfc_se *se, int - zero_cond); + cond); @@ -6345,0 +6347,4 @@ fcncall_realloc_result (gfc_se *se, int + + zero_cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR, + boolean_type_node, zero_cond, + cond); ! Based on the example of PR 52117 by Steven Hirshman PROGRAM RESHAPEIT INTEGER, PARAMETER :: n1=2, n2=2, n3=2 INTEGER :: m1, m2, m3, lc REAL, ALLOCATABLE :: A(:,:), B(:,:,:) REAL :: val ALLOCATE (A(n1,n2*n3)) ! <<< No allocation: Segfault ! ALLOCATE (B(1,1,1)) ! << Does not segfault, but result is wrong ! ALLOCATE (B(n1,n2,n3)) ! << WORKS val = 0 lc = 0 DO m3=1,n3 DO m2=1,n2 lc = lc+1 DO m1=1,n1 val = val+1 A(m1, lc) = val END DO END DO END DO B = RESHAPE(A, [n1,n2,n3]) lc = 0 DO m3=1,n3 DO m2=1,n2 lc = lc+1 DO m1=1,n1 PRINT *,'A(',m1,',',lc,') = ',A(m1,lc),' B = ',B(m1,m2,m3) if (A(m1,lc) /= B(m1,m2,m3)) call abort () END DO END DO END DO DEALLOCATE(A, B) END PROGRAM RESHAPEIT