[PATCH] Fix ICEs in *.mod writer

Jakub Jelinek jakub@redhat.com
Mon Mar 12 17:41:00 GMT 2007


Hi!

The attached testcase crashes in the *.mod writer code, because ts->cl
(of BT_REAL type of __convert_*_* symbol) contains garbage.
Some gfc_typespecs have initialized all 4 fields (kind/type/derived/cl),
but unfortunately many don't, e.g. in iresolve.c I see from quick skimming
19 places that only initialize kind/type and 12 that initialize all 4.
match_complex_constant is another case that only initializes kind/type.
mio_typespec only touches ts->derived if BT_DERIVED, but unconditionally
looks at ts->cl and so when it contains garbage, the compiler can segfault,
or SIGBUS, or e.g. write random part of memory as gfc_charlen.

I think best would be to make gfc_typespec one pointer smaller, merge
derived and cl into a union and in all places initialize it, but that would
be quite a big patch.

The attached patch is IMHO desirable anyway even if that is changed, as ts->u.cl
shouldn't be looked at least for BT_DERIVED (because it will be
ts->u.derived in that case).

Another fix would be to make a gfc_typespec to_ts = *ts;
copy in gfc_convert_type_warn and clear the fields that callers don't
bother to initialize:
  if (ts->type != BT_CHARACTER)
    to_ts.cl = NULL;
  if (ts->type != BT_DERIVED)
    to_ts.derived = NULL;

Ok for 4.3/4.2/4.1?

2007-03-12  Jakub Jelinek  <jakub@redhat.com>

	* module.c (mio_typespec): Don't look at ts->cl if not BT_CHARACTER.

	* gfortran.dg/module_implicit_conversion.f90: New test.

--- gcc/fortran/module.c.jj	2007-03-12 17:17:56.000000000 +0100
+++ gcc/fortran/module.c	2007-03-12 17:54:56.000000000 +0100
@@ -1814,7 +1814,14 @@ mio_typespec (gfc_typespec *ts)
   else
     mio_symbol_ref (&ts->derived);
 
-  mio_charlen (&ts->cl);
+  if (ts->type != BT_CHARACTER)
+    {
+      /* ts->cl is only valid for BT_CHARACTER.  */
+      mio_lparen ();
+      mio_rparen ();
+    }
+  else
+    mio_charlen (&ts->cl);
 
   mio_rparen ();
 }
--- gcc/testsuite/gfortran.dg/module_implicit_conversion.f90.jj	2007-03-12 18:02:29.000000000 +0100
+++ gcc/testsuite/gfortran.dg/module_implicit_conversion.f90	2007-03-12 18:06:20.000000000 +0100
@@ -0,0 +1,9 @@
+! { dg-do compile }
+! { dg-options "-std=gnu" }
+
+module module_implicit_conversion
+  ! double complex :: s = (1.0D0, 0D0) 
+  double complex :: s = (1.0, 0D0)
+end module module_implicit_conversion
+
+! { dg-final { cleanup-modules "module_implicit_conversion" } }

	Jakub



More information about the Gcc-patches mailing list