Taken from http://www.polyhedron.com/pb05/linux/diagnose.html ------<CBND1.F>------- ! character bound error - local variable program cbnd1 character*10 zz i = 2 j = i+9 zz(i:j) = 'abcdef' print * , zz end ---------------------- gfortran-4.2 -Wall -pedantic -fbounds-check CBND1.F ./a.out ^Aabcdef Expected: Issue a run-time error message. Examples of other compilers: ifort64-9.1 -C -check all -warn all,nodec,interfaces -gen_interfaces -traceback -fpe0 -fpstkchk CBND1.F ./a.out forrtl: severe (408): fort: (4): Variable ZZ has substring ending point 11 which is greater than the variable length of 10 g95 -Wall -pedantic -fbounds-check -ftrace=full CBND1.F ./a.out At line 6 of file CBND1.F Traceback: (Innermost first) Fortran runtime error: Substring reference out of bounds: (2:11) in string of length 10 NAG: f95 -C=all -C=undefined -info -g -gline CBND1.F Out of range: substring ending position 11 is greater than length 10 Program terminated by fatal error In CBND1, line 6 of CBND1.F The following is even detected at compile time by the intel and NAGf95 compilers (gfortran does not detect it at compile or runtime): --------<CBND3.F>-------------- ! Check character bound error - assign to dummy argument which is larger than actual arg program cbnd3 character*10 zz,zzz common /aaa/zz,zzz zz = '0000000000' zzz = '1111111111' call sub(zz) print * , zzz end subroutine sub(zz) character*11 zz zz = 'abcdef' print * , zz end ---------------------------
Tobias, You are right, we do not check character lengths in assignment, yet. It is another PR but I just do not recall which, right now. You are correct, that this should be included in bounds checking. The standard demands that the length of the rhs, in total, should not exceed that of the lhs. Thank you for the report. Paul
I'm writing a patch to add substring bounds checking. I hope to post it in the next few days.
> From Francois-Xavier Coudert 2006-06-08 I'm writing a patch to add substring bounds checking. I hope to post it in the next few days. What is the status? If you have something, I'd save my time of looking at trans-expr.c's gfc_conv_substring and trying to understand how to add it there. Looking at http://www.polyhedron.com/pb05/linux/diagnose.html, this seems to be the last area where gfortran is worse than all others.
Subject: Re: -fbounds-check should catch substring out of range accesses > I'm writing a patch to add substring bounds checking. I hope to post it in the > next few days. Great! > What is the status? If you have something, I'd save my time of looking > at trans-expr.c's gfc_conv_substring and trying to understand how to > add it there. The status is: "I had the beginning of a patch, but it didn't work fully, and I think it didn't survive summer cleaning of my account". But, I'll be happy to answer any question you have, and review your patch.
Subject: Bug number PR27588 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00234.html
The latest patch is at: http://gcc.gnu.org/ml/gcc-patches/2006-10/msg01030.html Problems: - variable name missing - line number is off-by-one
Mine.
Subject: Bug number PR27588 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-11/msg00694.html
Subject: Bug 27588 Author: burnus Date: Wed Nov 15 10:13:16 2006 New Revision: 118852 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118852 Log: fortran/ 2006-11-15 Tobias Burnus <burnus@net-b.de> Francois-Xavier Coudert <fxcoudert@gcc.gnu.org> PR fortran/27588 * trans-expr.c (gfc_conv_substring): Add bounds checking. (gfc_conv_variable, gfc_conv_substring_expr): Pass more arguments to gfc_conv_substring. testsuite/ 2006-11-15 Tobias Burnus <burnus@net-b.de> PR fortran/27588 * gfortran.dg/char_bounds_check_fail_1.f90: New test. Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/trans-expr.c trunk/gcc/testsuite/ChangeLog
Fixed in 4.3 == trunk.
Subject: Bug 27588 Author: burnus Date: Wed Jan 31 10:23:53 2007 New Revision: 121401 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=121401 Log: (This part was missing in the r118852 / Wed Nov 15 10:13:16 2006 check in) 2007-01-31 Tobias Burnus <burnus@net-b.de> PR fortran/27588 * gfortran.dg/char_bounds_check_fail_1.f90: Add test. Added: trunk/gcc/testsuite/gfortran.dg/char_bounds_check_fail_1.f90 Modified: trunk/gcc/testsuite/ChangeLog