Bug 96025 - [10/11/12/13 Regression] ICE in expr_check_typed_help, at fortran/expr.c:5437
Summary: [10/11/12/13 Regression] ICE in expr_check_typed_help, at fortran/expr.c:5437
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 11.0
: P4 normal
Target Milestone: 10.5
Assignee: anlauf
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks: 96024
  Show dependency treegraph
 
Reported: 2020-07-01 19:31 UTC by G. Steinmetz
Modified: 2023-03-05 19:38 UTC (History)
1 user (show)

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


Attachments
Tentative patch (832 bytes, patch)
2023-02-19 21:02 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-07-01 19:31:48 UTC
Affects versions down to r6, similar to pr96024 :


$ cat z1.f90
program p
   print *, f()
contains
   character(char(1)) function f()
      f = 'f'
   end
end


$ gfortran-5 -c z1.f90
z1.f90:4:13:

    character(char(1)) function f()
             1
Error: Expression at (1) must be of INTEGER type, found CHARACTER


$ gfortran-11-20200628 -c z1.f90
f951: internal compiler error: Segmentation fault
0xbd215f crash_signal
        ../../gcc/toplev.c:328
0x6592b0 expr_check_typed_help
        ../../gcc/fortran/expr.c:5437
0x65f90d gfc_traverse_expr(gfc_expr*, gfc_symbol*, bool (*)(gfc_expr*, gfc_symbol*, int*), int)
        ../../gcc/fortran/expr.c:5212
0x65f956 gfc_traverse_expr(gfc_expr*, gfc_symbol*, bool (*)(gfc_expr*, gfc_symbol*, int*), int)
        ../../gcc/fortran/expr.c:5262
0x65fa01 gfc_traverse_expr(gfc_expr*, gfc_symbol*, bool (*)(gfc_expr*, gfc_symbol*, int*), int)
        ../../gcc/fortran/expr.c:5219
0x65ff5e gfc_expr_check_typed(gfc_expr*, gfc_namespace*, bool)
        ../../gcc/fortran/expr.c:5475
0x6b13af parse_spec
        ../../gcc/fortran/parse.c:3829
0x6b3f0c parse_progunit
        ../../gcc/fortran/parse.c:5852
0x6b42f1 parse_contained
        ../../gcc/fortran/parse.c:5753
0x6b4196 parse_progunit
        ../../gcc/fortran/parse.c:5924
0x6b55e9 gfc_parse_file()
        ../../gcc/fortran/parse.c:6393
0x7016ff gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:212
Comment 1 G. Steinmetz 2020-07-01 19:32:15 UTC
Also related :


$ cat z2.f90
module m
contains
   character(char(1)) function f()
      f = 'f'
   end
end
program p
   use m
   print *, f()
end


$ gfortran-11-20200628 -c z2.f90
f951: internal compiler error: in gfc_traverse_expr, at fortran/expr.c:5269
0x65fcbf gfc_traverse_expr(gfc_expr*, gfc_symbol*, bool (*)(gfc_expr*, gfc_symbol*, int*), int)
        ../../gcc/fortran/expr.c:5269
0x65fa01 gfc_traverse_expr(gfc_expr*, gfc_symbol*, bool (*)(gfc_expr*, gfc_symbol*, int*), int)
        ../../gcc/fortran/expr.c:5219
0x65ff5e gfc_expr_check_typed(gfc_expr*, gfc_namespace*, bool)
        ../../gcc/fortran/expr.c:5475
0x6b13af parse_spec
        ../../gcc/fortran/parse.c:3829
0x6b3f0c parse_progunit
        ../../gcc/fortran/parse.c:5852
0x6b42f1 parse_contained
        ../../gcc/fortran/parse.c:5753
0x6b5127 parse_module
        ../../gcc/fortran/parse.c:6126
0x6b5457 gfc_parse_file()
        ../../gcc/fortran/parse.c:6429
0x7016ff gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:212
Comment 2 Dominique d'Humieres 2020-07-01 20:19:17 UTC
GCC11 and GCC7 give

pr96025.f90:4:13:

    4 |    character(char(1)) function f()
      |             1
Error: Expression at (1) must be of INTEGER type, found CHARACTER

but the instrumented compiler gives

==71986==ERROR: AddressSanitizer: heap-use-after-free on address 0x604000000f90 at pc 0x00010015d5e6 bp 0x7ffeefbfe490 sp 0x7ffeefbfe488
READ of size 8 at 0x604000000f90 thread T0
    #0 0x10015d5e5 in gfc_traverse_expr(gfc_expr*, gfc_symbol*, bool (*)(gfc_expr*, gfc_symbol*, int*), int) expr.c:5217
    #1 0x1001709cb in gfc_expr_check_typed(gfc_expr*, gfc_namespace*, bool) expr.c:5475
    #2 0x1003575e0 in check_function_result_typed() parse.c:3720
    #3 0x10036b22d in parse_spec(gfc_statement) parse.c:3827
    #4 0x100371f06 in parse_progunit(gfc_statement) parse.c:5852
    #5 0x100372910 in parse_contained(int) parse.c:5753
    #6 0x1003724c5 in parse_progunit(gfc_statement) parse.c:5924
    #7 0x10037465f in gfc_parse_file() parse.c:6393
    #8 0x10055a224 in gfc_be_parse_file() f95-lang.c:212
    #9 0x106b46254 in compile_file() toplev.c:458
    #10 0x106b552e3 in do_compile() toplev.c:2307
    #11 0x10a42f19e in toplev::main(int, char**) toplev.c:2446
    #12 0x10a933d44 in main main.c:39
    #13 0x7fff6bb23cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8)

0x604000000f90 is located 0 bytes inside of 48-byte region [0x604000000f90,0x604000000fc0)
freed by thread T0 here:
    #0 0x15b8378f7 in wrap_free.part.0+0x97 (libasan.6.dylib:x86_64+0x4a8f7)
    #1 0x10050d6db in gfc_free_charlen(gfc_charlen*, gfc_charlen*) symbol.c:3990
    #2 0x10050da95 in gfc_free_namespace(gfc_namespace*) symbol.c:4041
    #3 0x1000d5a47 in gfc_match_char_spec(gfc_typespec*) decl.c:3506
    #4 0x1000f171d in gfc_match_decl_type_spec(gfc_typespec*, int) decl.c:4166
    #5 0x1000f59ce in gfc_match_prefix(gfc_typespec*) decl.c:6317
    #6 0x100364d2d in match_deferred_characteristics(gfc_typespec*) parse.c:3666
    #7 0x10036ba1e in parse_spec(gfc_statement) parse.c:3936
    #8 0x100371f06 in parse_progunit(gfc_statement) parse.c:5852
    #9 0x100372910 in parse_contained(int) parse.c:5753
    #10 0x1003724c5 in parse_progunit(gfc_statement) parse.c:5924
    #11 0x10037465f in gfc_parse_file() parse.c:6393
    #12 0x10055a224 in gfc_be_parse_file() f95-lang.c:212
    #13 0x106b46254 in compile_file() toplev.c:458
    #14 0x106b552e3 in do_compile() toplev.c:2307
    #15 0x10a42f19e in toplev::main(int, char**) toplev.c:2446
    #16 0x10a933d44 in main main.c:39
    #17 0x7fff6bb23cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8)

previously allocated by thread T0 here:
    #0 0x15b837fff in wrap_calloc+0xbf (libasan.6.dylib:x86_64+0x4afff)
    #1 0x109833754 in xcalloc xmalloc.c:162
    #2 0x10050b457 in gfc_new_charlen(gfc_namespace*, gfc_charlen*) symbol.c:3954
    #3 0x10044a874 in fixup_charlen(gfc_expr*) resolve.c:6078
    #4 0x10041538e in gfc_resolve_expr(gfc_expr*) resolve.c:7108
    #5 0x1001650df in gfc_reduce_init_expr(gfc_expr*) expr.c:3085
    #6 0x1000d59b5 in gfc_match_char_spec(gfc_typespec*) decl.c:3496
    #7 0x1000f171d in gfc_match_decl_type_spec(gfc_typespec*, int) decl.c:4166
    #8 0x1000f59ce in gfc_match_prefix(gfc_typespec*) decl.c:6317
    #9 0x100364d2d in match_deferred_characteristics(gfc_typespec*) parse.c:3666
    #10 0x10036ba1e in parse_spec(gfc_statement) parse.c:3936
    #11 0x100371f06 in parse_progunit(gfc_statement) parse.c:5852
    #12 0x100372910 in parse_contained(int) parse.c:5753
    #13 0x1003724c5 in parse_progunit(gfc_statement) parse.c:5924
    #14 0x10037465f in gfc_parse_file() parse.c:6393
    #15 0x10055a224 in gfc_be_parse_file() f95-lang.c:212
    #16 0x106b46254 in compile_file() toplev.c:458
    #17 0x106b552e3 in do_compile() toplev.c:2307
    #18 0x10a42f19e in toplev::main(int, char**) toplev.c:2446
    #19 0x10a933d44 in main main.c:39
    #20 0x7fff6bb23cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8)


For GCC8 to GCC10 I get a

(null):0: confused by earlier errors, bailing out

(release builds)/
Comment 3 kargls 2020-07-01 21:21:23 UTC
This fixes the ICE.  Patch is against svn revision 280156.

e-ts.u.cl is a garbage pointer.

Index: gcc/fortran/expr.c
===================================================================
--- gcc/fortran/expr.c	(revision 280157)
+++ gcc/fortran/expr.c	(working copy)
@@ -3447,6 +3447,7 @@ gfc_specification_expr (gfc_expr *e)
     {
       gfc_error ("Expression at %L must be of INTEGER type, found %s",
 		 &e->where, gfc_basic_typename (e->ts.type));
+      gfc_clear_ts (&e->ts);
       return false;
     }
Comment 4 Dominique d'Humieres 2020-07-04 09:28:39 UTC
> This fixes the ICE.

Confirmed, with the patch I get

Error: Expression at (1) must be of INTEGER type, found CHARACTER
Comment 5 Richard Biener 2021-06-01 08:17:58 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Comment 6 Richard Biener 2022-05-27 09:43:01 UTC
GCC 9 branch is being closed
Comment 7 Jakub Jelinek 2022-06-28 10:41:11 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Comment 8 anlauf 2023-02-19 21:02:45 UTC
Created attachment 54488 [details]
Tentative patch

The attached patch adds some hopefully more reasonable error recovery.
Regtests cleanly on x86_64-pc-linux-gnu and is valgrind clean.
Comment 10 GCC Commits 2023-02-21 18:04:54 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:6c1b825b3d6499dfeacf7c79dcf4b56a393ac204

commit r13-6265-g6c1b825b3d6499dfeacf7c79dcf4b56a393ac204
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Mon Feb 20 21:28:09 2023 +0100

    Fortran: improve checking of character length specification [PR96025]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/96025
            * parse.cc (check_function_result_typed): Improve type check of
            specification expression for character length and return status.
            (parse_spec): Use status from above.
            * resolve.cc (resolve_fntype): Prevent use of invalid specification
            expression for character length.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/96025
            * gfortran.dg/pr96025.f90: New test.
Comment 11 GCC Commits 2023-03-04 19:51:29 UTC
The releases/gcc-12 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

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

commit r12-9220-gabd1571d2340b5c86ba4d51ef9affc743e764ba8
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Mon Feb 20 21:28:09 2023 +0100

    Fortran: improve checking of character length specification [PR96025]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/96025
            * parse.cc (check_function_result_typed): Improve type check of
            specification expression for character length and return status.
            (parse_spec): Use status from above.
            * resolve.cc (resolve_fntype): Prevent use of invalid specification
            expression for character length.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/96025
            * gfortran.dg/pr96025.f90: New test.
    
    (cherry picked from commit 6c1b825b3d6499dfeacf7c79dcf4b56a393ac204)
Comment 12 GCC Commits 2023-03-05 19:35:38 UTC
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:8412f78621f6dcb7cee23dda49afb28299d8083b

commit r11-10558-g8412f78621f6dcb7cee23dda49afb28299d8083b
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Mon Feb 20 21:28:09 2023 +0100

    Fortran: improve checking of character length specification [PR96025]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/96025
            * parse.c (check_function_result_typed): Improve type check of
            specification expression for character length and return status.
            (parse_spec): Use status from above.
            * resolve.c (resolve_fntype): Prevent use of invalid specification
            expression for character length.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/96025
            * gfortran.dg/pr96025.f90: New test.
    
    (cherry picked from commit 6c1b825b3d6499dfeacf7c79dcf4b56a393ac204)
Comment 13 GCC Commits 2023-03-05 19:36:44 UTC
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:9db1287c8ced5425f6ef9d26b05a3eb9cbcc4b8d

commit r10-11239-g9db1287c8ced5425f6ef9d26b05a3eb9cbcc4b8d
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Mon Feb 20 21:28:09 2023 +0100

    Fortran: improve checking of character length specification [PR96025]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/96025
            * parse.c (check_function_result_typed): Improve type check of
            specification expression for character length and return status.
            (parse_spec): Use status from above.
            * resolve.c (resolve_fntype): Prevent use of invalid specification
            expression for character length.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/96025
            * gfortran.dg/pr96025.f90: New test.
    
    (cherry picked from commit 6c1b825b3d6499dfeacf7c79dcf4b56a393ac204)
Comment 14 anlauf 2023-03-05 19:38:25 UTC
Fixed on all open branches.  Closing.

Thanks for the report!