Bug 93366 - ICE: Invalid expression in gfc_element_size
Summary: ICE: Invalid expression in gfc_element_size
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 10.0
: P4 normal
Target Milestone: ---
Assignee: anlauf
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2020-01-21 17:52 UTC by G. Steinmetz
Modified: 2020-06-14 12:58 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-01-21 00:00:00


Attachments
Update and extension of Steve's patch (1.13 KB, patch)
2020-04-30 20:43 UTC, anlauf
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description G. Steinmetz 2020-01-21 17:52:02 UTC
Affects gfortran down to at least r5; null() is not allowed here :


$ cat z1.f90
program p
   print *, sizeof(null())
end


$ gfortran-10-20200119 -c z1.f90
f951: internal compiler error: Invalid expression in gfc_element_size.
0x647599 gfc_report_diagnostic
        ../../gcc/fortran/error.c:782
0x648cba gfc_internal_error(char const*, ...)
        ../../gcc/fortran/error.c:1402
0x6e4582 gfc_element_size(gfc_expr*, unsigned long*)
        ../../gcc/fortran/target-memory.c:137
0x6e45e3 gfc_target_expr_size(gfc_expr*, unsigned long*)
        ../../gcc/fortran/target-memory.c:166
0x6d85ab gfc_simplify_sizeof(gfc_expr*)
        ../../gcc/fortran/simplify.c:7417
0x65c99f do_simplify
        ../../gcc/fortran/intrinsic.c:4603
0x66721a gfc_intrinsic_func_interface(gfc_expr*, int)
        ../../gcc/fortran/intrinsic.c:4996
0x6bea5e resolve_unknown_f
        ../../gcc/fortran/resolve.c:2894
0x6bea5e resolve_function
        ../../gcc/fortran/resolve.c:3238
0x6bea5e gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.c:7000
0x6b5dec gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.c:6967
0x6b5dec gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.c:11688
0x6c4cdf gfc_resolve_blocks(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.c:10715
0x6b4b18 gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.c:11678
0x6b7397 resolve_codes
        ../../gcc/fortran/resolve.c:17205
0x6b745e gfc_resolve(gfc_namespace*)
        ../../gcc/fortran/resolve.c:17240
0x6a57bc resolve_all_program_units
        ../../gcc/fortran/parse.c:6241
0x6a57bc gfc_parse_file()
        ../../gcc/fortran/parse.c:6488
0x6f044f gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:210
Comment 1 G. Steinmetz 2020-01-21 17:52:38 UTC

Similar :


$ cat z2.f90
program p
   print *, transfer(1, null())
   print *, transfer([1], null())
end


$ cat z3.f90
program p
   print *, transfer (null(), 1)
   print *, transfer (null(), [1])
end


$ cat z4.f90
program p
   print *, transfer(null(), null())
end


$ gfortran-10-20200119 -c z3.f90
f951: internal compiler error: Invalid expression in gfc_element_size.
0x647599 gfc_report_diagnostic
        ../../gcc/fortran/error.c:782
0x648cba gfc_internal_error(char const*, ...)
        ../../gcc/fortran/error.c:1402
0x6e4582 gfc_element_size(gfc_expr*, unsigned long*)
        ../../gcc/fortran/target-memory.c:137
0x6e45e3 gfc_target_expr_size(gfc_expr*, unsigned long*)
        ../../gcc/fortran/target-memory.c:166
0x623a6a gfc_calculate_transfer_sizes(gfc_expr*, gfc_expr*, gfc_expr*, unsigned long*, unsigned long*, unsigned long*)
        ../../gcc/fortran/check.c:6083
0x6d971d gfc_simplify_transfer(gfc_expr*, gfc_expr*, gfc_expr*)
        ../../gcc/fortran/simplify.c:7926
0x65ca07 do_simplify
        ../../gcc/fortran/intrinsic.c:4617
0x66721a gfc_intrinsic_func_interface(gfc_expr*, int)
        ../../gcc/fortran/intrinsic.c:4996
0x6bea5e resolve_unknown_f
        ../../gcc/fortran/resolve.c:2894
0x6bea5e resolve_function
        ../../gcc/fortran/resolve.c:3238
0x6bea5e gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.c:7000
0x6b5dec gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.c:6967
0x6b5dec gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.c:11688
0x6c4cdf gfc_resolve_blocks(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.c:10715
0x6b4b18 gfc_resolve_code(gfc_code*, gfc_namespace*)
        ../../gcc/fortran/resolve.c:11678
0x6b7397 resolve_codes
        ../../gcc/fortran/resolve.c:17205
0x6b745e gfc_resolve(gfc_namespace*)
        ../../gcc/fortran/resolve.c:17240
0x6a57bc resolve_all_program_units
        ../../gcc/fortran/parse.c:6241
0x6a57bc gfc_parse_file()
        ../../gcc/fortran/parse.c:6488
0x6f044f gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:210
Comment 2 kargl 2020-01-21 19:07:02 UTC
patch against last SVN revision.

Index: gcc/fortran/check.c
===================================================================
--- gcc/fortran/check.c	(revision 280157)
+++ gcc/fortran/check.c	(working copy)
@@ -1426,6 +1426,18 @@ gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
   return true;
 }
 
+static bool
+invalid_null_arg (gfc_expr *x)
+{
+  if (x->expr_type == EXPR_NULL)
+    {
+      gfc_error ("NULL pointer at %L is not permitted as actual argument "
+		 "of %qs intrinsic function", &x->where,
+		 gfc_current_intrinsic);
+      return true;
+    }
+  return false;
+}
 
 bool
 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
@@ -1433,13 +1445,10 @@ gfc_check_associated (gfc_expr *pointer, gfc_expr *tar
   symbol_attribute attr1, attr2;
   int i;
   bool t;
-  locus *where;
 
-  where = &pointer->where;
+  if (invalid_null_arg (pointer))
+    return false;
 
-  if (pointer->expr_type == EXPR_NULL)
-    goto null_arg;
-
   attr1 = gfc_expr_attr (pointer);
 
   if (!attr1.pointer && !attr1.proc_pointer)
@@ -1463,9 +1472,8 @@ gfc_check_associated (gfc_expr *pointer, gfc_expr *tar
   if (target == NULL)
     return true;
 
-  where = &target->where;
-  if (target->expr_type == EXPR_NULL)
-    goto null_arg;
+  if (invalid_null_arg (target))
+    return false;
 
   if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
     attr2 = gfc_expr_attr (target);
@@ -1513,13 +1521,6 @@ gfc_check_associated (gfc_expr *pointer, gfc_expr *tar
 	  }
     }
   return t;
-
-null_arg:
-
-  gfc_error ("NULL pointer at %L is not permitted as actual argument "
-	     "of %qs intrinsic function", where, gfc_current_intrinsic);
-  return false;
-
 }
 
 
@@ -5124,6 +5125,9 @@ gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_ex
 bool
 gfc_check_sizeof (gfc_expr *arg)
 {
+  if (invalid_null_arg (arg))
+    return false;
+
   if (arg->ts.type == BT_PROCEDURE)
     {
       gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
@@ -6139,6 +6143,9 @@ gfc_check_transfer (gfc_expr *source, gfc_expr *mold, 
   size_t source_size;
   size_t result_size;
 
+  if (invalid_null_arg (source))
+    return false;
+
   /* SOURCE shall be a scalar or array of any type.  */
   if (source->ts.type == BT_PROCEDURE
       && source->symtree->n.sym->attr.subroutine == 1)
@@ -6153,6 +6160,9 @@ gfc_check_transfer (gfc_expr *source, gfc_expr *mold, 
     return false;
 
   if (mold->ts.type == BT_BOZ && illegal_boz_arg (mold))
+    return false;
+
+  if (invalid_null_arg (mold))
     return false;
 
   /* MOLD shall be a scalar or array of any type.  */
Comment 3 anlauf 2020-04-29 20:57:44 UTC
(In reply to kargl from comment #2)
> patch against last SVN revision.
Steve,

do you still care?
Comment 4 Steve Kargl 2020-04-29 21:20:53 UTC
On Wed, Apr 29, 2020 at 08:57:44PM +0000, anlauf at gcc dot gnu.org wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93366
> 
> anlauf at gcc dot gnu.org changed:
> 
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>                  CC|                            |anlauf at gcc dot gnu.org
> 
> --- Comment #3 from anlauf at gcc dot gnu.org ---
> (In reply to kargl from comment #2)
> > patch against last SVN revision.
> Steve,
> 
> do you still care?
> 

It is not that I don't care.  It is that I no longer have
an ability to commit patches or checkout top-of-tree.  The
recent io.c discuss that I participated in shows that top-of-tree
and svn r280157 have started to diverge substantially.  So,
my time is done.  It is time for a younger generation to 
step up.
Comment 5 anlauf 2020-04-30 20:43:04 UTC
Created attachment 48427 [details]
Update and extension of Steve's patch

I've updated Steve's patch to reflect current master before creating the
10-release branch.

My additions handle a few accepts-invalid cases for
KIND, MERGE, SHAPE, SPREAD.

If there are no objections, I will submit with a testcase after the 10 release
for the 11 master.
Comment 6 anlauf 2020-05-04 20:16:24 UTC
Patch posted at

https://gcc.gnu.org/pipermail/gcc-patches/2020-May/545091.html
Comment 7 CVS Commits 2020-05-05 20:17:15 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:5a26ea7e0f8b9a00a2eb0a5e8f70efa04056f167

commit r11-120-g5a26ea7e0f8b9a00a2eb0a5e8f70efa04056f167
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue May 5 22:16:50 2020 +0200

    PR fortran/93366 - ICE on invalid, reject invalid use of NULL() as argument
    
    gcc/fortran/ChangeLog:
    
    2020-05-05  Steve Kargl  <kargl@gcc.gnu.org>
            Harald Anlauf  <anlauf@gmx.de>
    
            PR fortran/93366
            * check.c (gfc_check_associated, invalid_null_arg): Factorize
            check for presence of invalid NULL() argument.
            (gfc_check_kind, gfc_check_merge, gfc_check_shape)
            (gfc_check_sizeof, gfc_check_spread, gfc_check_transfer): Use this
            check for presence of invalid NULL() arguments.
    
    gcc/testsuite/ChangeLog:
    
    2020-05-05  Harald Anlauf  <anlauf@gmx.de>
    
            PR fortran/93366
            * gfortran.dg/pr93366.f90: New test.
Comment 8 anlauf 2020-05-05 20:32:33 UTC
Fixed on master for GCC 11.  Thanks for the report!
Comment 9 CVS Commits 2020-06-14 12:58:32 UTC
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:294105192c9f6bdcebce81752fc1e64e2b7adebb

commit r10-8301-g294105192c9f6bdcebce81752fc1e64e2b7adebb
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue May 5 22:16:50 2020 +0200

    PR fortran/93366 - ICE on invalid, reject invalid use of NULL() as argument
    
    gcc/fortran/ChangeLog:
    
    2020-05-05  Steve Kargl  <kargl@gcc.gnu.org>
                Harald Anlauf  <anlauf@gmx.de>
    
            PR fortran/93366
            * check.c (gfc_check_associated, invalid_null_arg): Factorize
            check for presence of invalid NULL() argument.
            (gfc_check_kind, gfc_check_merge, gfc_check_shape)
            (gfc_check_sizeof, gfc_check_spread, gfc_check_transfer): Use this
            check for presence of invalid NULL() arguments.
    
    gcc/testsuite/ChangeLog:
    
    2020-05-05  Harald Anlauf  <anlauf@gmx.de>
    
            PR fortran/93366
            * gfortran.dg/pr93366.f90: New test.
    
    (cherry picked from commit 5a26ea7e0f8b9a00a2eb0a5e8f70efa04056f167)