This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH, Fortran, pr79230, v1] [7 Regression] [OOP] Run time error: double free or corruption


Hi all,

attached patch fixes a regression when a polymorphic pointer component was
present. The results was a double free. The attached patch fixes this, by not
caring about freeing pointer components as part of gfortran's memory management,
i.e., the programmer has to take care about freeing/disassociating the pointer
using a finalizer, as is IMO the intention of the Fortran standard.

Bootstrapped and regtested ok on x86_64-linux/f25. Ok for trunk (current or
next, haven't monitored whether commits are still allowed)?

Regards,
	Andre
-- 
Andre Vehreschild * Email: vehre ad gmx dot de 

Attachment: pr79230_v1.clog
Description: Text document

diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c
index a3aab8e..d0dfc26 100644
--- a/gcc/fortran/trans-array.c
+++ b/gcc/fortran/trans-array.c
@@ -8220,9 +8220,17 @@ structure_alloc_comps (gfc_symbol * der_type, tree decl,
 
 	  /* Shortcut to get the attributes of the component.  */
 	  if (c->ts.type == BT_CLASS)
-	    attr = &CLASS_DATA (c)->attr;
+	    {
+	      attr = &CLASS_DATA (c)->attr;
+	      if (attr->class_pointer)
+		continue;
+	    }
 	  else
-	    attr = &c->attr;
+	    {
+	      attr = &c->attr;
+	      if (attr->pointer)
+		continue;
+	    }
 
 	  if ((c->ts.type == BT_DERIVED && !c->attr.pointer)
 	     || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer))
diff --git a/gcc/testsuite/gfortran.dg/der_ptr_component_2.f90 b/gcc/testsuite/gfortran.dg/der_ptr_component_2.f90
new file mode 100644
index 0000000..4eb0869
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/der_ptr_component_2.f90
@@ -0,0 +1,30 @@
+! { dg-do run }
+!
+! Freeing the width_data lead to double free. This testcase tests that
+! pr79230 is fixed now.
+
+program main_ut
+  implicit none
+
+  type :: data_t
+     character, allocatable :: c1
+  end type
+
+  type :: t1_t
+     character, allocatable :: c2
+     class(data_t), pointer :: width_data
+  end type
+
+  call evaluator
+
+contains
+
+  subroutine evaluator
+    type(data_t), target :: par_real
+    type(t1_t) :: field
+    field%width_data => par_real
+  end subroutine
+
+end
+
+

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]