The following produces a segfault at runtime with -O2 and higher

module m
  type t
    real x
  contains
    procedure, pass :: a
  end type
contains
  real function a (arg)
    class (t) :: arg
    a = arg%x
  end function

  real function b (arg)
    real :: arg
    b = arg*2
  end function

  subroutine s
    class(t), allocatable :: z
    allocate(z)          ! see tree-optimized output
    z%x = 42             ! see tree-optimized output
    print *, b (z%a())   ! see tree-optimized output
  end subroutine
end module

  use m
  call s
end

With -O0, the marked lines give
<bb 2>:
  D.1348_1 = malloc (4);
  if (D.1348_1 == 0B)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 3>:
  _gfortran_os_error (&"Out of memory"[1]{lb: 1 sz: 1});

<bb 4>:
  D.1374_2 = (struct t *) D.1348_1;
  z.$data = D.1374_2;
  z.$vindex = 2800126;
  z.$size = 4;
  D.1375_3 = z.$data;
  D.1375_3->x = 4.2e+1;
  dt_parm.0.common.filename = &"test.f03"[1]{lb: 1 sz: 1};
  dt_parm.0.common.line = 22;
  dt_parm.0.common.flags = 128;
  dt_parm.0.common.unit = 6;
  _gfortran_st_write (&dt_parm.0);
  D.1376_4 = __m_MOD_a (&z);
  D.1350 = D.1376_4;
  D.1377_5 = __m_MOD_b (&D.1350);
  D.1351 = D.1377_5;
  _gfortran_transfer_real (&dt_parm.0, &D.1351, 4);
  _gfortran_st_write_done (&dt_parm.0);

whilst with -O2
<bb 2>:
  D.1348_1 = malloc (4);
  if (D.1348_1 == 0B)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 3>:
  _gfortran_os_error (&"Out of memory"[1]{lb: 1 sz: 1});

<bb 4>:
  D.1374_2 = (struct t *) D.1348_1;
  dt_parm.0.common.filename = &"test.f03"[1]{lb: 1 sz: 1};
  dt_parm.0.common.line = 22;
  dt_parm.0.common.flags = 128;
  dt_parm.0.common.unit = 6;
  _gfortran_st_write (&dt_parm.0);
  arg_21 = (struct .class.t & restrict) &z;
  D.1389_22 = arg_21->$data;
  D.1386_23 = D.1389_22->x;
  D.1392_26 = D.1386_23 * 2.0e+0;
  D.1351 = D.1392_26;
  _gfortran_transfer_real (&dt_parm.0, &D.1351, 4);
  _gfortran_st_write_done (&dt_parm.0);


ie. the important part of the allocation and the entire assignment disappear!
Since the PRINT then tries to access the data component of the CLASS object,
the segfault results


-- 
           Summary: OOP - segfault with -O2 using methods as actual
                    arguments
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: pault at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41753

Reply via email to