[Bug fortran/106603] New: Problem with character(:), allocatable, intent(out) :: err for functions which return fixed arrays

wogan at uw dot edu gcc-bugzilla@gcc.gnu.org
Fri Aug 12 21:42:39 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106603

            Bug ID: 106603
           Summary: Problem with character(:), allocatable, intent(out) ::
                    err for functions which return fixed arrays
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wogan at uw dot edu
  Target Milestone: ---

Consider the following code:

```fortran
program main
  use iso_fortran_env, only: dp => real64
  implicit none
  call test_fun1()

contains

  function fun1(a, err) result(b)
    real(dp), intent(in) :: a
    character(:), allocatable, intent(out) :: err
    real(dp) :: b(2)

    b = 1

    if (a > 2) then
      err = "a bigger than 2"
      print*,'err in fun1: ',err
      return
    endif

    err = "OK"

  end function

  subroutine test_fun1()
    real(dp) :: a, b(2)
    character(:), allocatable :: err
    a = 3

    b = fun1(a, err)
    if (allocated(err)) then
      print*,'err is allocated'
      print*,'next line tries to print err'
      print*,err
    endif
  end subroutine

end program
```

I compile the code with `gfortran main.f90`, then run with `./a.out`. The
result is

```
(base) nicholas@Nicholass-MacBook-Air test_gfortran_err % ./a.out          
 err in fun1: a bigger than 2
 err is allocated
 next line tries to print err

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x100df21c7
#1  0x100df11a3
#2  0x194092c43
#3  0x100ee865b
zsh: segmentation fault  ./a.out
```

When I compile with `-fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations` I get a slightly different result, which is
also not correct:

```
(base) nicholas@Nicholass-MacBook-Air test_gfortran_err % ./a.out               
 err in fun1: a bigger than 2
 err is allocated
 next line tries to print err


```

Instead of the results above, the program should print "a bigger than 2". Here
is a relevant form where I discussed the bug with others:
https://fortran-lang.discourse.group/t/what-is-wrong-with-this-code/4151 . 

Exact version of GCC:
```
(base) nicholas@Nicholass-MacBook-Air test_gfortran_err % gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/12.1.0/bin/../libexec/gcc/aarch64-apple-darwin20/12/lto-wrapper
Target: aarch64-apple-darwin20
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc
--libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls
--enable-checking=release --with-gcc-major-version-only
--enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12
--with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr
--with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl
--with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.1.0'
--with-bugurl=https://github.com/Homebrew/homebrew-core/issues
--with-system-zlib --build=aarch64-apple-darwin20
--with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (Homebrew GCC 12.1.0) 
```


More information about the Gcc-bugs mailing list