[Bug fortran/104100] Passing an allocated array to a C bind function alters the bounds

2022-01-18 Thread hzhou321 at anl dot gov via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104100

--- Comment #2 from Hui Zhou  ---
Great! That means it has already been fixed in the coming 12 series, right? I
was initially a bit confused with your WONTFIX comment.

[Bug fortran/104100] New: Passing an allocated array to a C bind function alters the bounds

2022-01-18 Thread hzhou321 at anl dot gov via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104100

Bug ID: 104100
   Summary: Passing an allocated array to a C bind function alters
the bounds
   Product: gcc
   Version: 11.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: hzhou321 at anl dot gov
  Target Milestone: ---

https://github.com/pmodels/mpich/issues/4170#issuecomment-1015580478

Minimum reproducer:
`t.f90`:
```
program t
INTERFACE
SUBROUTINE F_cdesc(buf) bind(C, name="F_cdesc")
IMPLICIT NONE
TYPE(*), DIMENSION(..), INTENT(in) :: buf
END SUBROUTINE
END INTERFACE

! Only reproducible with allocatable array. "INTEGER:: A(5)" would be fine.
INTEGER, allocatable, dimension(:):: A

allocate(A(5))

A = (/1, 2, 3, 4, 5/);
print*, A
write(*,'(a6,2(1x,i2))') 'Before:', lbound(A), ubound(A)

! "CALL F_cdesc(A)" would be fine
CALL f08ts(A)

print*, A
write(*,'(a6,2(1x,i2))') 'After:', lbound(A), ubound(A)

deallocate(A)

contains
SUBROUTINE f08ts(buf)
IMPLICIT NONE
TYPE(*), DIMENSION(..), INTENT(in) :: buf
call F_cdesc(buf)
END SUBROUTINE f08ts
end
```
`cdesc.c`:
```
#include 

void F_cdesc(CFI_cdesc_t *buf) { }
```
Run with:
```
gcc-9 -c -o cdesc.o cdesc.c && gfortran-9 t.f90 cdesc.o -o t && ./t
```

Results:
```
   1   2   3   4   5
Before  1  5
   1   2   3   4   5
After:  0  4
```

EDIT: also reproduced with `gcc 11`