[gfortran,patch] Don't let character strings have negative length

FX Coudert fxcoudert@gmail.com
Sun Mar 18 09:13:00 GMT 2007

Hi all,

Among the new PRs opened by Joost, most are related to a few areas:  
TRANSFER, allocatable & pointers, implicit typing and symbol  
resolution, and CHARACTER lengths.

PR31203 is one of the CHARACTER bugs, where we can initialize a  
character length variable to be negative in code such as the following:
   subroutine foo(i)
     character(len=i) :: s
   end subroutine foo
   call foo(-2)

In fact, I think there's a single spot where this should be changed;  
this leads to the 2-lines patch below. It was bootstrapped and  
regtested on i686-linux. It will be commited along with the necessary  
ChangeLog entry and a few testcases, both from the PR and my own  
creations :)

OK for mainline and 4.2 when it unfreezes?


PS: I intend to work (as time permit) on the other CHARACTER-length  
related issues, from Joost's bug reports, another one from Thomas K.  
and a few others by me. The code handling them seems to need some  
serious dusting, especially the end of the match_char_spec()  
function, in decl.c.

:ADDPATCH fortran:

Index: gcc/fortran/trans-expr.c
--- gcc/fortran/trans-expr.c    (revision 123028)
+++ gcc/fortran/trans-expr.c    (working copy)
@@ -227,6 +227,8 @@

    gfc_init_se (&se, NULL);
    gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
+  se.expr = fold_build2 (MAX_EXPR, gfc_charlen_type_node, se.expr,
+                        build_int_cst (gfc_charlen_type_node, 0));
    gfc_add_block_to_block (pblock, &se.pre);

    tmp = cl->backend_decl;

More information about the Gcc-patches mailing list