[Bug fortran/100651] Weird memory corruption with multiple triggers

gscfq@t-online.de gcc-bugzilla@gcc.gnu.org
Thu May 20 10:23:00 GMT 2021


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

--- Comment #2 from G. Steinmetz <gscfq@t-online.de> ---

Simplified a bit :


$ cat z1.f90
module m
   type :: t
   contains
      procedure, pass(this) :: assign_to_string
      generic :: assignment(=) => assign_to_string
   end type
contains
   subroutine assign_to_string(string, this)
      character(:), allocatable, intent(out) :: string
      class(t), intent(in) :: this
   end
   subroutine assert_code(err_msg)
      character(:), optional, allocatable, intent(inout) :: err_msg
      err_msg = 'foo bar'
   end
   function to_int(err_msg) result(ptr)
      integer :: ptr
      character(:), optional, allocatable, intent(inout) :: err_msg
      ptr = 1
      call assert_code(err_msg)
   end
end
program main
   use m
   type, extends(t) :: t2
      real(8) :: value = 1
   end type
   type(t2) :: x
   integer :: n
   character(:), allocatable :: err_msg
   x = t2(2)
   n = to_int(err_msg)
   print *, 'allocated :', allocated(err_msg)
   print *, 'len :      ', len(err_msg)
!  print *, 'len_trim : ', len_trim(err_msg)
   print *, 'err_msg :  ', err_msg
end


$ cat z2.f90
module m
   type :: t
   contains
      procedure, pass(this) :: assign_to_string
      generic :: assignment(=) => assign_to_string
   end type
contains
   subroutine assign_to_string(string, this)
      character(:), allocatable, intent(out) :: string
      class(t), intent(in) :: this
   end
   subroutine assert_code(err_msg)
      character(:), allocatable, intent(inout) :: err_msg
      err_msg = 'foo bar'
   end
   function to_int(err_msg) result(ptr)
      integer :: ptr
      character(:), allocatable, intent(inout) :: err_msg
      ptr = 1
      call assert_code(err_msg)
   end
end
program main
   use m
   type, extends(t) :: t2
      real(8) :: value = 1
   end type
   type(t2) :: x
   integer :: n
   character(:), allocatable :: err_msg
   x = t2(2)
   n = to_int(err_msg)
   print *, 'allocated :', allocated(err_msg)
   print *, 'len :      ', len(err_msg)
!  print *, 'len_trim : ', len_trim(err_msg)
   print *, 'err_msg :  ', err_msg
end



$ gfortran-12-20210516 z1.f90 -g -O0 && ./a.out
 allocated : T
 len :                 0
Operating system error: Cannot allocate memory
Memory allocation failure in xrealloc

Error termination. Backtrace:
#0  0x40d1c7 in write_character
        at ../../../libgfortran/io/write.c:1416
#1  0x41222e in list_formatted_write_scalar
        at ../../../libgfortran/io/write.c:1900
#2  0x40265e in MAIN__
        at .../z1.f90:36
#3  0x4026a4 in main
        at .../z1.f90:24


$ gfortran-12-20210516 z2.f90 -g -O0 && ./a.out
 allocated : T
 len :                 7
 err_msg :  foo bar


More information about the Gcc-bugs mailing list