Bug 88328 - ICE in resolve_tag_format, at fortran/io.c:1641
Summary: ICE in resolve_tag_format, at fortran/io.c:1641
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 9.0
: P4 normal
Target Milestone: 9.0
Assignee: kargls
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-03 18:51 UTC by G. Steinmetz
Modified: 2018-12-23 05:21 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-12-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description G. Steinmetz 2018-12-03 18:51:29 UTC
Affects versions down to at least gcc-5 :


$ cat z1.f90
program p
   character(3), parameter :: a(0) = [character(3)::]
   print a
end


$ gfortran-9-20181202 -c z1.f90
f951: internal compiler error: Segmentation fault
0xb2ec9f crash_signal
        ../../gcc/toplev.c:326
0x63ddee resolve_tag_format
        ../../gcc/fortran/io.c:1641
0x63ddee resolve_tag
        ../../gcc/fortran/io.c:1747
0x641f37 gfc_resolve_dt(gfc_dt*, locus*)
        ../../gcc/fortran/io.c:3239
0x6860a7 gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.c:11579
0x688b0f resolve_codes
        ../../gcc/fortran/resolve.c:16704
0x688bde gfc_resolve(gfc_namespace*)
        ../../gcc/fortran/resolve.c:16739
0x676967 resolve_all_program_units
        ../../gcc/fortran/parse.c:6067
0x676967 gfc_parse_file()
        ../../gcc/fortran/parse.c:6317
0x6bf5ff gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:204
Comment 1 kargls 2018-12-03 21:08:58 UTC
(In reply to G. Steinmetz from comment #0)
> Affects versions down to at least gcc-5 :
> 
> 
> $ cat z1.f90
> program p
>    character(3), parameter :: a(0) = [character(3)::]
>    print a
> end
> 
> 
> $ gfortran-9-20181202 -c z1.f90
> f951: internal compiler error: Segmentation fault
> 0xb2ec9f crash_signal
>         ../../gcc/toplev.c:326
> 0x63ddee resolve_tag_format
>         ../../gcc/fortran/io.c:1641

I get

% gfcx -c a.f90
a.f90:3:10:

    3 |    print a
      |          1
Error: FORMAT tag at (1) cannot be a zero-sized array

with this patch

Index: gcc/fortran/io.c
===================================================================
--- gcc/fortran/io.c	(revision 266710)
+++ gcc/fortran/io.c	(working copy)
@@ -1636,6 +1636,12 @@ resolve_tag_format (gfc_expr *e)
 	  gfc_expr *r;
 	  gfc_char_t *dest, *src;
 
+	  if (e->value.constructor == NULL)
+	    {
+	      gfc_error ("FORMAT tag at %C cannot be a zero-sized array");
+	      return false;
+	    }
+
 	  n = 0;
 	  c = gfc_constructor_first (e->value.constructor);
 	  len = c->expr->value.character.length;
@@ -3231,12 +3237,17 @@ gfc_resolve_dt (gfc_dt *dt, locus *loc)
 {
   gfc_expr *e;
   io_kind k;
+  locus loc_tmp;
 
   /* This is set in any case.  */
   gcc_assert (dt->dt_io_kind);
   k = dt->dt_io_kind->value.iokind;
 
+  loc_tmp = gfc_current_locus;
+  gfc_current_locus = *loc;
   RESOLVE_TAG (&tag_format, dt->format_expr);
+  gfc_current_locus = loc_tmp;
+
   RESOLVE_TAG (&tag_rec, dt->rec);
   RESOLVE_TAG (&tag_spos, dt->pos);
   RESOLVE_TAG (&tag_advance, dt->advance);
Comment 2 kargls 2018-12-04 15:57:19 UTC
(In reply to kargl from comment #1)
> (In reply to G. Steinmetz from comment #0)
> > Affects versions down to at least gcc-5 :
> > 
> > 
> > $ cat z1.f90
> > program p
> >    character(3), parameter :: a(0) = [character(3)::]
> >    print a
> > end
> > 
> > 
> > $ gfortran-9-20181202 -c z1.f90
> > f951: internal compiler error: Segmentation fault
> > 0xb2ec9f crash_signal
> >         ../../gcc/toplev.c:326
> > 0x63ddee resolve_tag_format
> >         ../../gcc/fortran/io.c:1641
> 
> I get
> 
> % gfcx -c a.f90
> a.f90:3:10:
> 
>     3 |    print a
>       |          1
> Error: FORMAT tag at (1) cannot be a zero-sized array
> 
> with this patch
> 
> Index: gcc/fortran/io.c
> ===================================================================
> --- gcc/fortran/io.c	(revision 266710)
> +++ gcc/fortran/io.c	(working copy)
> @@ -1636,6 +1636,12 @@ resolve_tag_format (gfc_expr *e)
>  	  gfc_expr *r;
>  	  gfc_char_t *dest, *src;
>  
> +	  if (e->value.constructor == NULL)
> +	    {
> +	      gfc_error ("FORMAT tag at %C cannot be a zero-sized array");
> +	      return false;
> +	    }
> +
>  	  n = 0;
>  	  c = gfc_constructor_first (e->value.constructor);
>  	  len = c->expr->value.character.length;
> @@ -3231,12 +3237,17 @@ gfc_resolve_dt (gfc_dt *dt, locus *loc)
>  {
>    gfc_expr *e;
>    io_kind k;
> +  locus loc_tmp;
>  
>    /* This is set in any case.  */
>    gcc_assert (dt->dt_io_kind);
>    k = dt->dt_io_kind->value.iokind;
>  
> +  loc_tmp = gfc_current_locus;
> +  gfc_current_locus = *loc;
>    RESOLVE_TAG (&tag_format, dt->format_expr);
> +  gfc_current_locus = loc_tmp;
> +
>    RESOLVE_TAG (&tag_rec, dt->rec);
>    RESOLVE_TAG (&tag_spos, dt->pos);
>    RESOLVE_TAG (&tag_advance, dt->advance);

The patch is slightly wrong.  RESOLVE_TAG expands to 'if () return false',
so on failure gfc_current_locus is not reset.
Comment 3 kargls 2018-12-23 05:18:59 UTC
Author: kargl
Date: Sun Dec 23 05:18:27 2018
New Revision: 267367

URL: https://gcc.gnu.org/viewcvs?rev=267367&root=gcc&view=rev
Log:
2018-12-22  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/88328
	* io.c (resolve_tag_format): Add error for zero-sized array.
	(gfc_resolve_dt): Manipulate gfc_current_locus to get sensible error
	message locus.

2018-12-22  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/88328
	* gfortran.dg/pr88328.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/pr88328.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/io.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 kargls 2018-12-23 05:21:16 UTC
Fixed on trunk.  Closing.