Bug 57596 - Wrong code for allocatable deferred-length strings
Wrong code for allocatable deferred-length strings
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.9.0
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
: wrong-code
Depends on:
Blocks: 45170
  Show dependency treegraph
 
Reported: 2013-06-12 13:54 UTC by Valery Weber
Modified: 2013-06-14 07:42 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-06-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Valery Weber 2013-06-12 13:54:27 UTC
Dear All

The following code is producing a segfault with 
gcc version 4.9.0 20130612 (experimental) (GCC)
Is that a bug?
v



MODULE base_types
  TYPE :: base_integer_type
     INTEGER :: i
  END TYPE base_integer_type
  TYPE :: base_character_type
     CHARACTER( 10 ) :: c
  END TYPE base_character_type
END MODULE base_types

PROGRAM main
  USE base_types
  IMPLICIT NONE
  INTEGER::i_val
  call get (  i_val=i_val )
  write(*,*) 'i_val',i_val
contains

  SUBROUTINE get (i_val, c_val)
    INTEGER, INTENT( OUT ), OPTIONAL :: i_val
    CHARACTER( : ), INTENT( OUT ), ALLOCATABLE, OPTIONAL :: c_val
    CLASS( * ), POINTER :: p
    TYPE( base_integer_type ),target :: i_base
    i_base%i=-12
    p=>i_base
    SELECT TYPE( p )
    TYPE IS( base_integer_type )
       IF(present(i_val)) i_val = p%i
    TYPE IS( base_character_type )
       if(present(c_val)) c_val = p%c
    CLASS DEFAULT
       stop
    END SELECT
  END SUBROUTINE get
END PROGRAM main

./a.out 

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

Backtrace for this error:
#0  0x4120ED in _gfortrani_backtrace at backtrace.c:258
#1  0x401DD0 in _gfortrani_backtrace_handler at compile_options.c:129
#2  0x3FC8C35C1F
#3  0x401C08 in get.1883 at main.f90:?
#4  0x401B71 in MAIN__ at main.f90:?
Segmentation fault (core dumped)
Comment 1 Tobias Burnus 2013-06-12 16:22:44 UTC
Close as INVALID - feel free to re-open if you disagree.


Seems to be invalid. Fortran 2008 has in "8.1.3.3 Attributes of associate names":

"Within an ASSOCIATE or SELECT TYPE construct [...] If the selector has
the OPTIONAL attribute, it shall be present."
Comment 2 Valery Weber 2013-06-13 07:00:02 UTC
But the selector is not optional. The problem even remains if I 
move the optional variables outside the SELECT TYPE (see bellow).
Those 2 codes run just fine with other compilers like ifort or xlf.
Thanks
Valery


MODULE base_types
  TYPE :: base_integer_type
     INTEGER :: i
  END TYPE base_integer_type
  TYPE :: base_character_type
     CHARACTER( 10 ) :: c
  END TYPE base_character_type
END MODULE base_types

PROGRAM main
  USE base_types
  IMPLICIT NONE
  INTEGER::i_val
  call get (  i_val=i_val )
  write(*,*) 'i_val',i_val
contains

  SUBROUTINE get (i_val, c_val)
    INTEGER, INTENT( OUT ), OPTIONAL :: i_val
    CHARACTER( : ), INTENT( OUT ), ALLOCATABLE, OPTIONAL :: c_val
    CLASS( * ), POINTER :: p
    TYPE( base_integer_type ),target :: i_base
    INTEGER :: i_val_tmp
    CHARACTER( 10 ) :: c_val_tmp
    i_base%i=-12
    p=>i_base
    SELECT TYPE( p )
    TYPE IS( base_integer_type )
       i_val_tmp = p%i
    TYPE IS( base_character_type )
       c_val_tmp = p%c
    CLASS DEFAULT
       stop
    END SELECT
    IF(present(i_val)) i_val = i_val_tmp
    if(present(c_val)) c_val = c_val_tmp
  END SUBROUTINE get
END PROGRAM main
Comment 3 Tobias Burnus 2013-06-13 07:23:46 UTC
REOPEN

PROGRAM main
  IMPLICIT NONE
  call get ()
contains
  SUBROUTINE get (c_val)
    CHARACTER( : ), INTENT( OUT ), ALLOCATABLE, OPTIONAL :: c_val
    CHARACTER( 10 ) :: c_val_tmp
    if(present(c_val)) c_val = c_val_tmp
  END SUBROUTINE get
END PROGRAM main
Comment 4 Tobias Burnus 2013-06-13 09:14:05 UTC
Patch: http://gcc.gnu.org/ml/fortran/2013-06/msg00082.html
Comment 5 Tobias Burnus 2013-06-14 07:42:32 UTC
Author: burnus
Date: Fri Jun 14 07:41:42 2013
New Revision: 200084

URL: http://gcc.gnu.org/viewcvs?rev=200084&root=gcc&view=rev
Log:
2013-06-14  Tobias Burnus  <burnus@net-b.de>

        PR fortran/57596
        * trans-decl.c (gfc_trans_deferred_vars): Honor OPTIONAL
        for nullify and deferred-strings' length variable.

2013-06-14  Tobias Burnus  <burnus@net-b.de>

        PR fortran/57596
        * gfortran.dg/deferred_type_param_9.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/deferred_type_param_9.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Tobias Burnus 2013-06-14 07:42:58 UTC
FIXED on the trunk (4.9).

Thanks for the report!