Bug 47189 - [OOP] calling STORAGE_SIZE on a NULL-initialized class pointer
[OOP] calling STORAGE_SIZE on a NULL-initialized class pointer
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.6.0
: P3 normal
: ---
Assigned To: janus
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-01-06 13:15 UTC by janus
Modified: 2011-01-07 17:29 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-01-06 13:28:51


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description janus 2011-01-06 13:15:03 UTC
Follow-up to PR47180 / PR47024:

type :: t
  real(4) :: c
end type
class(t), pointer :: x => null()
print *, storage_size (x)
end

This currently gives a segfault at runtime, because the _vptr component of 'x' is wrongly initialized to NULL, while it should point to the vtab of the declared type. If one adds an additional pointer assignment statement

x => null()

everything works fine (due to the fix of PR47180). Only the NULL initialization still gets it wrong.
Comment 1 janus 2011-01-06 13:28:51 UTC
Patch:


Index: gcc/fortran/class.c
===================================================================
--- gcc/fortran/class.c	(revision 168525)
+++ gcc/fortran/class.c	(working copy)
@@ -83,7 +83,8 @@ gfc_add_component_ref (gfc_expr *e, const char *na
 
 
 /* Build a NULL initializer for CLASS pointers,
-   initializing the _data and _vptr components to zero.  */
+   initializing the _data component to NULL and
+   the _vptr component to the declared type.  */
 
 gfc_expr *
 gfc_class_null_initializer (gfc_typespec *ts)
@@ -98,9 +99,10 @@ gfc_class_null_initializer (gfc_typespec *ts)
   for (comp = ts->u.derived->components; comp; comp = comp->next)
     {
       gfc_constructor *ctor = gfc_constructor_get();
-      ctor->expr = gfc_get_expr ();
-      ctor->expr->expr_type = EXPR_NULL;
-      ctor->expr->ts = comp->ts;
+      if (strcmp (comp->name, "_vptr") == 0)
+	ctor->expr = gfc_lval_expr_from_sym (gfc_find_derived_vtab (ts->u.derived));
+      else
+	ctor->expr = gfc_get_null_expr (NULL);
       gfc_constructor_append (&init->value.constructor, ctor);
     }
Comment 2 janus 2011-01-07 12:08:48 UTC
Author: janus
Date: Fri Jan  7 12:08:21 2011
New Revision: 168565

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=168565
Log:
2011-01-07  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/47189
	PR fortran/47194
	* gfortran.h (gfc_lval_expr_from_sym): Moved prototype.
	* class.c (gfc_class_null_initializer): Initialize _vptr to declared
	type.
	* expr.c (gfc_lval_expr_from_sym): Moved here from symbol.c.
	* resolve.c (resolve_deallocate_expr): _data component will be added
	at translation stage.
	* symbol.c (gfc_lval_expr_from_sym): Moved to expr.c.
	* trans-stmt.c (gfc_trans_deallocate): Reset _vptr to declared type.


2011-01-07  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/47189
	PR fortran/47194
	* gfortran.dg/storage_size_3.f08: Extended.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/class.c
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/symbol.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/storage_size_3.f08
Comment 3 janus 2011-01-07 17:29:09 UTC
Fixed with r168565. Closing.