Bug 100274 - [9/10/11/12 Regression] ICE in gfc_conv_procedure_call, at fortran/trans-expr.c:6131
Summary: [9/10/11/12 Regression] ICE in gfc_conv_procedure_call, at fortran/trans-expr...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 12.0
: P4 normal
Target Milestone: 9.4
Assignee: anlauf
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2021-04-26 19:48 UTC by G. Steinmetz
Modified: 2021-05-05 19:59 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-04-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description G. Steinmetz 2021-04-26 19:48:53 UTC
Changed with r9 between 20180916 and 20180923 :
(as a supplement to pr100154)


$ cat z1.f90
program p
   call s('x')
contains
   subroutine s(x)
      character(8), intent(out) :: x
   end
end


$ gfortran-9-20180916 -c z1.f90
z1.f90:2:10:

2 |    call s('x')
  |          1
Warning: Character length of actual argument shorter than of dummy argument 'x' (1/8) at (1) [-Wargument-mismatch]


$ gfortran-11-20210425 -c z1.f90
z1.f90:2:10:

    2 |    call s('x')
      |          1
Warning: Character length of actual argument shorter than of dummy argument 'x' (1/8) at (1)
z1.f90:2:14:

    2 |    call s('x')
      |              1
internal compiler error: Segmentation fault
0xc0b78f crash_signal
        ../../gcc/toplev.c:327
0x77206f gfc_conv_procedure_call(gfc_se*, gfc_symbol*, gfc_actual_arglist*, gfc_expr*, vec<tree_node*, va_gc, vl_embed>*)
        ../../gcc/fortran/trans-expr.c:6131
0x7a9ba8 gfc_trans_call(gfc_code*, bool, tree_node*, tree_node*, bool)
        ../../gcc/fortran/trans-stmt.c:424
0x739e38 trans_code
        ../../gcc/fortran/trans.c:2000
0x760094 gfc_generate_function_code(gfc_namespace*)
        ../../gcc/fortran/trans-decl.c:6885
0x6e6a36 translate_all_program_units
        ../../gcc/fortran/parse.c:6355
0x6e6a36 gfc_parse_file()
        ../../gcc/fortran/parse.c:6624
0x732d2f gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:212
Comment 1 anlauf 2021-04-26 21:00:33 UTC
Confirmed.

Obvious fix for NULL pointer dereference:

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 213f32b0a67..450ee7e3ae7 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -6128,6 +6128,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
                      bool add_clobber;
                      add_clobber = fsym && fsym->attr.intent == INTENT_OUT
                        && !fsym->attr.allocatable && !fsym->attr.pointer
+                       && e->symtree
                        && !e->symtree->n.sym->attr.dimension
                        && !e->symtree->n.sym->attr.pointer
                        && !e->symtree->n.sym->attr.allocatable
Comment 2 anlauf 2021-04-27 20:39:21 UTC
The patch in comment#1 would turn the ICE into an accepts-invalid, since
we would only get a warning instead of an error.  This happens because
the character length check in gfc_compare_actual_formal returns too early
after emitting the warning.

The simplest fix would be to continue doing the checking:

diff --git a/gcc/fortran/interface.c b/gcc/fortran/interface.c
index 60736123550..9e3e8aa9da9 100644
--- a/gcc/fortran/interface.c
+++ b/gcc/fortran/interface.c
@@ -3255,10 +3255,13 @@ gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
          && f->sym->attr.flavor != FL_PROCEDURE)
        {
          if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
-           gfc_warning (0, "Character length of actual argument shorter "
-                        "than of dummy argument %qs (%lu/%lu) at %L",
-                        f->sym->name, actual_size, formal_size,
-                        &a->expr->where);
+           {
+             gfc_warning (0, "Character length of actual argument shorter "
+                          "than of dummy argument %qs (%lu/%lu) at %L",
+                          f->sym->name, actual_size, formal_size,
+                          &a->expr->where);
+             goto skip_size_check;
+           }
           else if (where)
            {
              /* Emit a warning for -std=legacy and an error otherwise. */

This regtests cleanly and allows to emit the same error message as for
matching character length.
Comment 4 GCC Commits 2021-05-05 13:26:22 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:a8b79cc939d6786293f654c42a2d1b0ab040de0e

commit r12-515-ga8b79cc939d6786293f654c42a2d1b0ab040de0e
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Wed May 5 15:25:50 2021 +0200

    PR fortran/100274 - ICE in gfc_conv_procedure_call, at fortran/trans-expr.c:6131
    
    When the check for the length of formal and actual character arguments
    found a mismatch and emitted a warning, it would skip further checks
    like that could lead to errors.  Fix that by continuing the checking.
    Also catch a NULL pointer dereference.
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100274
            * interface.c (gfc_compare_actual_formal): Continue checks after
            emitting warning for argument length mismatch.
            * trans-expr.c (gfc_conv_procedure_call): Check for NULL pointer
            dereference.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100274
            * gfortran.dg/argument_checking_25.f90: New test.
Comment 5 GCC Commits 2021-05-05 19:00:44 UTC
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:ba013672f723f87f0c3b0c685dda5b3b2f1b3f3a

commit r11-8357-gba013672f723f87f0c3b0c685dda5b3b2f1b3f3a
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Wed May 5 15:25:50 2021 +0200

    PR fortran/100274 - ICE in gfc_conv_procedure_call, at fortran/trans-expr.c:6131
    
    When the check for the length of formal and actual character arguments
    found a mismatch and emitted a warning, it would skip further checks
    like that could lead to errors.  Fix that by continuing the checking.
    Also catch a NULL pointer dereference.
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100274
            * interface.c (gfc_compare_actual_formal): Continue checks after
            emitting warning for argument length mismatch.
            * trans-expr.c (gfc_conv_procedure_call): Check for NULL pointer
            dereference.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100274
            * gfortran.dg/argument_checking_25.f90: New test.
    
    (cherry picked from commit a8b79cc939d6786293f654c42a2d1b0ab040de0e)
Comment 6 GCC Commits 2021-05-05 19:40:28 UTC
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:92fef3f29917407a31c58c1c06203e4fbe7d4319

commit r10-9803-g92fef3f29917407a31c58c1c06203e4fbe7d4319
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Wed May 5 15:25:50 2021 +0200

    PR fortran/100274 - ICE in gfc_conv_procedure_call, at fortran/trans-expr.c:6131
    
    When the check for the length of formal and actual character arguments
    found a mismatch and emitted a warning, it would skip further checks
    like that could lead to errors.  Fix that by continuing the checking.
    Also catch a NULL pointer dereference.
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100274
            * interface.c (gfc_compare_actual_formal): Continue checks after
            emitting warning for argument length mismatch.
            * trans-expr.c (gfc_conv_procedure_call): Check for NULL pointer
            dereference.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100274
            * gfortran.dg/argument_checking_25.f90: New test.
    
    (cherry picked from commit a8b79cc939d6786293f654c42a2d1b0ab040de0e)
Comment 7 GCC Commits 2021-05-05 19:55:53 UTC
The releases/gcc-9 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:d4a21a07125f73543ccc08fd96d4c41f37b6bd5d

commit r9-9514-gd4a21a07125f73543ccc08fd96d4c41f37b6bd5d
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Wed May 5 15:25:50 2021 +0200

    PR fortran/100274 - ICE in gfc_conv_procedure_call, at fortran/trans-expr.c:6131
    
    When the check for the length of formal and actual character arguments
    found a mismatch and emitted a warning, it would skip further checks
    like that could lead to errors.  Fix that by continuing the checking.
    Also catch a NULL pointer dereference.
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100274
            * interface.c (gfc_compare_actual_formal): Continue checks after
            emitting warning for argument length mismatch.
            * trans-expr.c (gfc_conv_procedure_call): Check for NULL pointer
            dereference.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100274
            * gfortran.dg/argument_checking_25.f90: New test.
    
    (cherry picked from commit a8b79cc939d6786293f654c42a2d1b0ab040de0e)
Comment 8 anlauf 2021-05-05 19:59:11 UTC
Fixed on mainline for gcc-12, and backported to 11-, 10- and 9-branches.
Closing.

Thanks for the report!