[Bug fortran/126381] New: Nullifying a deferred-length character pointer in a derived-type argument causes segmentation fault

mpahlow at geomar dot de via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126381

            Bug ID: 126381
           Summary: Nullifying a deferred-length character pointer in a
                    derived-type argument causes segmentation fault
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpahlow at geomar dot de
  Target Milestone: ---

The system type is macos tahoe 26.5.2 with CommandLineTools version
26.6.0.0.1781586589

gcc-16.1.0 was installed with brew

The following code produces the segmentation fault:

!file test.f90
MODULE mymodule
  IMPLICIT NONE
  INTEGER, PARAMETER :: dp=KIND(0D0)
  TYPE :: dimension
     CHARACTER(LEN=:), POINTER :: char_vals(:)
  END TYPE dimension
CONTAINS
  SUBROUTINE values_and_bounds (dimns)
    IMPLICIT NONE
!    TYPE(dimension), TARGET, INTENT(INOUT) :: dimns ! works
    CLASS(dimension), TARGET, INTENT(INOUT) :: dimns ! crashes
    write(*,*)'values_and_bounds: ','x'
    NULLIFY (dimns%char_vals)
    write(*,*)'values_and_bounds: ','y'
  END SUBROUTINE values_and_bounds
END MODULE mymodule

PROGRAM test
  USE mymodule
  IMPLICIT NONE
  TYPE(dimension) :: dimen
  NULLIFY (dimen%char_vals)
  write(*,*)'a'
  CALL values_and_bounds(dimns=dimen)
  WRITE(*,*)'b'
END PROGRAM test
! end of file test.f90

Command line:
gfortran -Og -Wall -Wextra -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -fsanitize=address,undefined -o test
test.f90; ./test

There is no output from the compiler but executing the executable shows:
a
 values_and_bounds: x
test.f90:13:29: runtime error: member access within null pointer of type
'struct dimension'

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

Backtrace for this error:
#0  0x10446b323
#1  0x10446a063
#2  0x18dc63743
#3  0x10423ce67
#4  0x10423d0cf
#5  0x10423d24f

Replacing "CLASS(dimension)" with "TYPE(dimension)" works.

Compiling with gfortran version 15.3.0 with the above command line makes the
executable hang but compiling with
gfortran-15 -Og -Wall -Wextra -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -o test test.f90; ./test
works
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.