[Patch,Fortran] PR 40452 Fix false positive with -fcheck=bounds

Tobias Burnus burnus@net-b.de
Tue Jun 16 22:29:00 GMT 2009


Hi all,

this is a follow up to yesterday's patch (PR 40383). (Which was approved
by Daniel (and is checked in), but for some reason Daniel's email only
appeared in gcc-patches@ but not in fortran@ although the header looks OK.)
(The issue below is quite independent from yesterday's patch, except that
 I found problem while looking at PR 40383.)


The problem with the original patch (PR 37746) was that it does not take
argument association fully into account. The following program is valid
but as the string lengths do not match, a bogus error is printed at run
time (with -fcheck=bounds) [compile-time diagnostics works as is should]

  program test
    implicit none
    call sub(["ab", "cd"])
  contains
    subroutine sub(a)
     character(len=4) :: a(1)
     print *, a(1)
    end subroutine sub
  end program test


The reason that the program is valid is due to the argument association,
which associates the storage of "ab" + "cd" with the dummy argument a(1).
As both have exactly 4 bytes, everything is OK.

Fortran 95 has (12.4.1.4 Sequence association)
"If the actual argument is of type default character and is an array
 expression, array element, or array element substring designator, the
 element sequence consists of the character storage units beginning with
 the first storage unit of the actual argument and continuing to the end
 of the array. The character storage units of an array element substring
 designator are viewed as array elements consisting of consecutive groups
 of character storage units having the character length of the dummy
 array."

(Note the: "dummy array" which is crucial for this patch.)

Fortran 2003 has in 12.4.1.5 the same (except of allowing C_CHAR) and
then continues with
"If the actual argument is of type default character or of type character
 with the C character kind, and is a scalar that is not an array element
 or array element substring designator, the element sequence consists of
 the storage units of the actual argument."

(The addition is not important for us, but it allows to pass "a string"
 to a BIND(C) procedure which has "character(len=1):: str(*)".)


The fix is simple: Don't do the checking if the dummy is an array, only
if it is a scalar. Fortunately, this is the normal use and thus the check
remains useful.


Bootstrapped and regtested on x86-64-linux.
OK for the trunk?

Tobias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: strlen.diff
Type: text/x-patch
Size: 2289 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20090616/dd59916b/attachment.bin>


More information about the Fortran mailing list