public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/25806]  New: problems with functions returning array pointers?
@ 2006-01-16 10:35 jpr at csc dot fi
  2006-01-16 16:30 ` [Bug fortran/25806] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: jpr at csc dot fi @ 2006-01-16 10:35 UTC (permalink / raw)
  To: gcc-bugs

Hi!

This code

program a 
  real, target :: storage(10)
  real :: s(5)

  print*,x(5)
  s = 0
  s = x(5)
  print*,s

contains

 function x(n) result(t)
   integer :: n
   real, pointer :: t(:)
   t => storage(1:n)
!  allocate( t(n) )
   t = (/ (i,i=1,n) /)
 end function x
end program a


prints:
free(): invalid pointer 0x5012a0!
   1.000000       2.000000       3.000000       4.000000       5.000000
   0.000000       0.000000       0.000000       0.000000       0.000000

on a x86_64. If you delete s=0 statement, the latter line will contain
values from uninitialized memory. 

gfortran --version
GNU Fortran 95 (GCC) 4.2.0 20060112 (experimental)
Copyright (C) 2005 Free Software Foundation, Inc.

Is this supposed to work?

Regards, Juha


-- 
           Summary: problems with functions returning array pointers?
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jpr at csc dot fi


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
@ 2006-01-16 16:30 ` pinskia at gcc dot gnu dot org
  2006-01-16 16:54 ` rguenth at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-01-16 16:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-01-16 16:30 -------
Confirmed, this is defintely a problem.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |wrong-code
   Last reconfirmed|0000-00-00 00:00:00         |2006-01-16 16:30:35
               date|                            |


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
  2006-01-16 16:30 ` [Bug fortran/25806] " pinskia at gcc dot gnu dot org
@ 2006-01-16 16:54 ` rguenth at gcc dot gnu dot org
  2006-02-04 17:11 ` eedelman at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-01-16 16:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2006-01-16 16:54 -------
Shorter testcase:

program a
  real, target :: storage(2)
  real :: s(2)

  s = x(2)
  print*,s

contains

 function x(n) result(t)
   integer :: n
   real, pointer :: t(:)
   t => storage(1:n)
!  allocate( t(n) )
   t = (/ (i,i=1,n) /)
 end function x

end program a


looks like the FE generates wrong trees:  .03.gimple is

MAIN__ ()
{ 
  real4[0:] * D.831;
  real4 storage[2];
  real4 s[2];
  static void x (struct array1_real4 &, int4 &);

  _gfortran_set_std (118, 127);
  { 
    int4 * D.828;
    int4 C.827 = 2;
    struct array1_real4 parm.3;

    parm.3.dtype = 281;
    parm.3.dim[0].lbound = 1;
    parm.3.dim[0].ubound = 2;
    parm.3.dim[0].stride = 1;
    D.831 = (real4[0:] *) &s[0];
    parm.3.data = D.831;
    parm.3.offset = 0;
    D.828 = &C.827;
    x (&parm.3, D.828);
  } 
  {
    struct __st_parameter_dt dt_parm.4;

    dt_parm.4.common.filename = "t3.f90";
    dt_parm.4.common.line = 6;
    dt_parm.4.common.unit = 6;
    dt_parm.4.common.flags = 128;
    _gfortran_st_write (&dt_parm.4);
    {
      struct array1_real4 parm.5;

      parm.5.dtype = 281;
      parm.5.dim[0].lbound = 1;
      parm.5.dim[0].ubound = 2;
      parm.5.dim[0].stride = 1;
      D.831 = (real4[0:] *) &s[0];
      parm.5.data = D.831;
      parm.5.offset = 0;
      _gfortran_transfer_array (&dt_parm.4, &parm.5, 4, 0);
    }
    _gfortran_st_write_done (&dt_parm.4);
  }
}

Note the call to _gfortran_st_write is before the array transfer.


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
  2006-01-16 16:30 ` [Bug fortran/25806] " pinskia at gcc dot gnu dot org
  2006-01-16 16:54 ` rguenth at gcc dot gnu dot org
@ 2006-02-04 17:11 ` eedelman at gcc dot gnu dot org
  2006-02-04 20:52 ` eedelman at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: eedelman at gcc dot gnu dot org @ 2006-02-04 17:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from eedelman at gcc dot gnu dot org  2006-02-04 17:11 -------
If I compile the original testcase with current mainline (revision 110561), the
binary dies on execution with 

*** glibc detected *** double free or corruption (out): 0xbfc9d020 ***
Aborted


If I remove the line 

     print*,x(5)

the program runs and outputs

0.000000       0.000000       0.000000       0.000000       0.000000

This trivial patch fixes the latter problem:

---------------------
Index: trans-expr.c
===================================================================
--- trans-expr.c        (revision 110590)
+++ trans-expr.c        (working copy)
@@ -2912,6 +2912,9 @@ gfc_trans_arrayfunc_assign (gfc_expr * e
   if (gfc_ref_needs_temporary_p (expr1->ref))
     return NULL;

+  if (expr2->symtree->n.sym->attr.pointer)
+    return NULL;
+
   /* Check that no LHS component references appear during an array
      reference. This is needed because we do not have the means to
      span any arbitrary stride with an array descriptor. This check
------------------------------------

I'll try to see if I can fix the other problem too.


-- 

eedelman at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |eedelman at gcc dot gnu dot
                   |dot org                     |org
             Status|NEW                         |ASSIGNED


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
                   ` (2 preceding siblings ...)
  2006-02-04 17:11 ` eedelman at gcc dot gnu dot org
@ 2006-02-04 20:52 ` eedelman at gcc dot gnu dot org
  2006-02-06 21:51 ` eedelman at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: eedelman at gcc dot gnu dot org @ 2006-02-04 20:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from eedelman at gcc dot gnu dot org  2006-02-04 20:52 -------
Created an attachment (id=10777)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=10777&action=view)
Patch to fix the bug.

We also get into trouble if we try to pass the result of a pointer-to-array
returning funtion as an argument to another procedure (e.g.

   call a_suborutine (x(5))

)

The problem, both for this case and

   print *, x(5)

is that we try to free the temporary used to hold the result. This we mustn't
do, since the temporary will be just a shallow copy of the resulting array.

The attached patch fixes these problems, but is not yet well tested. 


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
                   ` (3 preceding siblings ...)
  2006-02-04 20:52 ` eedelman at gcc dot gnu dot org
@ 2006-02-06 21:51 ` eedelman at gcc dot gnu dot org
  2006-02-12 17:34 ` eedelman at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: eedelman at gcc dot gnu dot org @ 2006-02-06 21:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from eedelman at gcc dot gnu dot org  2006-02-06 21:51 -------
A slightly improved form of the patch attached here earlier has been posted to
the mailing list for review:
http://gcc.gnu.org/ml/gcc-patches/2006-02/msg00394.html


-- 

eedelman at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|                            |http://gcc.gnu.org/ml/gcc-
                   |                            |patches/2006-
                   |                            |02/msg00394.html
           Keywords|                            |patch


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
                   ` (4 preceding siblings ...)
  2006-02-06 21:51 ` eedelman at gcc dot gnu dot org
@ 2006-02-12 17:34 ` eedelman at gcc dot gnu dot org
  2006-02-14 17:34 ` eedelman at gcc dot gnu dot org
  2006-02-14 20:31 ` eedelman at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: eedelman at gcc dot gnu dot org @ 2006-02-12 17:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from eedelman at gcc dot gnu dot org  2006-02-12 17:34 -------
Subject: Bug 25806

Author: eedelman
Date: Sun Feb 12 17:34:15 2006
New Revision: 110893

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=110893
Log:
fortran/
2006-02-12  Erik Edelmann  <eedelman@gcc.gnu.org>

        PR fortran/25806
        * trans-array.c (gfc_trans_allocate_array_storage): New argument
        dealloc; free the temporary only if dealloc is true.
        (gfc_trans_allocate_temp_array): New argument bool dealloc, to be 
        passed onwards to gfc_trans_allocate_array_storage.
        (gfc_trans_array_constructor, gfc_conv_loop_setup): Update call to
        gfc_trans_allocate_temp_array.
        * trans-array.h (gfc_trans_allocate_temp_array): Update function
        prototype.
        * trans-expr.c (gfc_conv_function_call): Set new argument 'dealloc'
        to gfc_trans_allocate_temp_array to false in case of functions
        returning pointers.
        (gfc_trans_arrayfunc_assign): Return NULL for functions returning
        pointers.


testsuite/
2006-02-12  Erik Edelmann  <eedelman@gcc.gnu.org>

        PR fortran/25806
        * gfortran.dg/ret_pointer_2.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/ret_pointer_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-array.h
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
                   ` (5 preceding siblings ...)
  2006-02-12 17:34 ` eedelman at gcc dot gnu dot org
@ 2006-02-14 17:34 ` eedelman at gcc dot gnu dot org
  2006-02-14 20:31 ` eedelman at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: eedelman at gcc dot gnu dot org @ 2006-02-14 17:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from eedelman at gcc dot gnu dot org  2006-02-14 17:34 -------
Subject: Bug 25806

Author: eedelman
Date: Tue Feb 14 17:34:07 2006
New Revision: 110989

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=110989
Log:
fortran/
2006-02-14  Erik Edelmann  <eedelman@gcc.gnu.org>

        PR fortran/25806
        * trans-array.c (gfc_trans_allocate_array_storage): New argument
        dealloc; free the temporary only if dealloc is true.
        (gfc_trans_allocate_temp_array): New argument bool dealloc, to be
        passed onwards to gfc_trans_allocate_array_storage.
        (gfc_trans_array_constructor, gfc_conv_loop_setup): Update call to
        gfc_trans_allocate_temp_array.
        * trans-array.h (gfc_trans_allocate_temp_array): Update function
        prototype.
        * trans-expr.c (gfc_conv_function_call): Set new argument 'dealloc'
        to gfc_trans_allocate_temp_array to false in case of functions
        returning pointers.
        (gfc_trans_arrayfunc_assign): Return NULL for functions returning
        pointers.

testsuite/
2006-02-14  Erik Edelmann  <eedelman@gcc.gnu.org>

        PR fortran/25806
        * gfortran.dg/ret_pointer_2.f90: New test.


Added:
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/ret_pointer_2.f90
      - copied unchanged from r110893,
trunk/gcc/testsuite/gfortran.dg/ret_pointer_2.f90
Modified:
    branches/gcc-4_1-branch/gcc/fortran/ChangeLog
    branches/gcc-4_1-branch/gcc/fortran/trans-array.c
    branches/gcc-4_1-branch/gcc/fortran/trans-array.h
    branches/gcc-4_1-branch/gcc/fortran/trans-expr.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/25806] problems with functions returning array pointers?
  2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
                   ` (6 preceding siblings ...)
  2006-02-14 17:34 ` eedelman at gcc dot gnu dot org
@ 2006-02-14 20:31 ` eedelman at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: eedelman at gcc dot gnu dot org @ 2006-02-14 20:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from eedelman at gcc dot gnu dot org  2006-02-14 20:31 -------
Fixed on 4.1 and mainline.


-- 

eedelman at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-02-14 20:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-16 10:35 [Bug fortran/25806] New: problems with functions returning array pointers? jpr at csc dot fi
2006-01-16 16:30 ` [Bug fortran/25806] " pinskia at gcc dot gnu dot org
2006-01-16 16:54 ` rguenth at gcc dot gnu dot org
2006-02-04 17:11 ` eedelman at gcc dot gnu dot org
2006-02-04 20:52 ` eedelman at gcc dot gnu dot org
2006-02-06 21:51 ` eedelman at gcc dot gnu dot org
2006-02-12 17:34 ` eedelman at gcc dot gnu dot org
2006-02-14 17:34 ` eedelman at gcc dot gnu dot org
2006-02-14 20:31 ` eedelman at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).