Bug 40851 - [4.3/4.4/4.5] problem with deallocation of pointers
Summary: [4.3/4.4/4.5] problem with deallocation of pointers
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: 4.3.5
Assignee: Tobias Burnus
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2009-07-24 20:03 UTC by Juergen Reuter
Modified: 2009-08-23 16:53 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-07-25 19:14:43


Attachments
main program for pointer output (189 bytes, application/octet-stream)
2009-07-24 20:04 UTC, Juergen Reuter
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Juergen Reuter 2009-07-24 20:03:05 UTC
gfortran seems to deallocate a pointer which was not allocated or need not be allocated. The program below compiles, but when executed returns the following error message:
a.out(8814) malloc: *** error for object 0x1fa2: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
Bus error
(Happens for Linux x86 and x86-64 as well as MAC OS X)
The code is:
program main

  type :: string
     character,dimension(:),allocatable :: chars
  end type string

  type :: string_container
     type(string) :: string
  end type string_container

  type(string_container), pointer :: ptr

  call set_ptr (ptr)

contains

  subroutine set_ptr (ptr)
    type(string_container), pointer, intent(out) :: ptr
    ptr => null ()
  end subroutine set_ptr

end program main
Comment 1 Juergen Reuter 2009-07-24 20:04:40 UTC
Created attachment 18249 [details]
main program for pointer output
Comment 2 Tobias Burnus 2009-07-25 06:55:40 UTC
CONFIRM. Affects all GCC with pointer intent (4.3/4.4/4.5). For

  subroutine set_ptr (ptr)
    type(string_container), pointer, intent(out) :: ptr

one shall not deallocate the pointer. The pointer intent is different from a non-pointer intent:


set_ptr (struct string_container * & ptr)
{
  if (ptr != 0)
    {
      if (ptr->string.chars.data != 0B)
        {
          __builtin_free (ptr->string.chars.data);


Untested patch:

--- trans-decl.c        (Revision 150038)
+++ trans-decl.c        (Arbeitskopie)
@@ -2958,6 +3009,7 @@ init_intent_out_dt (gfc_symbol * proc_sy
   gfc_init_block (&fnblock);
   for (f = proc_sym->formal; f; f = f->next)
     if (f->sym && f->sym->attr.intent == INTENT_OUT
+         && !sym->attr.pointer
          && f->sym->ts.type == BT_DERIVED)
       {
        if (f->sym->ts.derived->attr.alloc_comp)
@@ -3708,6 +3760,7 @@ generate_local_decl (gfc_symbol * sym)
       if (!sym->attr.referenced
            && sym->ts.type == BT_DERIVED
            && sym->ts.derived->attr.alloc_comp
+           && !sym->attr.pointer
            && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
                  ||
                (sym->attr.result && sym != sym->result)))
Comment 3 Tobias Burnus 2009-07-27 09:32:38 UTC
Subject: Bug 40851

Author: burnus
Date: Mon Jul 27 09:32:20 2009
New Revision: 150108

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=150108
Log:
2009-07-26  Tobias Burnus  <burnus@net-b.de>

        PR fortran/40851
        * resolve.c (resolve_symbol): Do not initialize pointer
        * derived-types.
        * trans-decl.c (init_intent_out_dt): Ditto.
        (generate_local_decl): No need to set attr.referenced for DT pointers.

2009-07-26  Tobias Burnus  <burnus@net-b.de>

        PR fortran/40851
        * gfortran.dg/derived_init_3.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/derived_init_3.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 4 Tobias Burnus 2009-07-27 09:33:19 UTC
Thanks for the report. FIXED on the trunk (4.5); I will soon backport it to 4.4/4.3.

(Workaround: Do not use INTENT for pointers [which is a Fortran 2003 feature].)
Comment 5 Tobias Burnus 2009-07-29 09:35:31 UTC
Subject: Bug 40851

Author: burnus
Date: Wed Jul 29 09:35:15 2009
New Revision: 150203

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=150203
Log:
2009-07-29  Tobias Burnus  <burnus@net-b.de>

        PR fortran/40851
        * resolve.c (resolve_symbol): Do not initialize pointer
        * derived-types.
        * trans-decl.c (init_intent_out_dt): Ditto.
        (generate_local_decl): No need to set attr.referenced for DT pointers.

2009-07-29  Tobias Burnus  <burnus@net-b.de>

        PR fortran/40851
        * gfortran.dg/derived_init_3.f90: New test.


Added:
    branches/gcc-4_4-branch/gcc/testsuite/gfortran.dg/derived_init_3.f90
Modified:
    branches/gcc-4_4-branch/gcc/fortran/ChangeLog
    branches/gcc-4_4-branch/gcc/fortran/resolve.c
    branches/gcc-4_4-branch/gcc/fortran/trans-decl.c
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog

Comment 6 Tobias Burnus 2009-08-16 20:29:20 UTC
Subject: Bug 40851

Author: burnus
Date: Sun Aug 16 20:29:02 2009
New Revision: 150813

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=150813
Log:
2009-08-16  Tobias Burnus  <burnus@net-b.de>

        PR fortran/40851
        * resolve.c (resolve_symbol): Do not initialize pointer
        * derived-types.
        * trans-decl.c (init_intent_out_dt): Ditto.
        (generate_local_decl): No need to set attr.referenced for DT pointers.

2009-08-16  Tobias Burnus  <burnus@net-b.de>

        PR fortran/40851
        * gfortran.dg/derived_init_3.f90: New test.


Modified:
    branches/gcc-4_3-branch/gcc/fortran/ChangeLog
    branches/gcc-4_3-branch/gcc/fortran/resolve.c
    branches/gcc-4_3-branch/gcc/fortran/trans-decl.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 7 Tobias Burnus 2009-08-16 20:29:34 UTC
FIXED on the 4.3 branch -> Close.
Comment 8 Mikael Pettersson 2009-08-23 11:09:44 UTC
(In reply to comment #7)
> FIXED on the 4.3 branch -> Close.

Minor nit: the commit to 4.3 didn't add the test case.

Comment 9 Tobias Burnus 2009-08-23 16:01:29 UTC
Subject: Bug 40851

Author: burnus
Date: Sun Aug 23 16:01:11 2009
New Revision: 151034

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=151034
Log:
2009-08-23  Tobias Burnus  <burnus@net-b.de>

        PR fortran/40851
        * gfortran.dg/derived_init_3.f90: New test.


Added:
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/derived_init_3.f90
Modified:
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 10 Tobias Burnus 2009-08-23 16:02:01 UTC
(In reply to comment #8)
> (In reply to comment #7)
> > FIXED on the 4.3 branch -> Close.
> Minor nit: the commit to 4.3 didn't add the test case.

I forgot to do a "svn add" - and I forgot to do a "svn status" to find such problems. It is not corrected.

Comment 11 Mikael Pettersson 2009-08-23 16:53:20 UTC
(In reply to comment #10)
> (In reply to comment #8)
> > (In reply to comment #7)
> > > FIXED on the 4.3 branch -> Close.
> > Minor nit: the commit to 4.3 didn't add the test case.
> 
> I forgot to do a "svn add" - and I forgot to do a "svn status" to find such
> problems. It is not corrected.

Thanks