Since the fix of PR 37746, gfortran's -fbounds-check is
able to diagnose actual arguments with too short string lengths
if the dummy argument has an explicit length, e.g. passing
call proc("abc")
to
subroutine sub(a)
character(len=4) :: a
The testing is done by comparing the argument:
if (_a < 4) runtime_error
For optional arguments, there is an additional condition.
That works quite nice, but it fails for:
subroutine sub(a,b)
character(len=4), optional :: a, b
The reason is that the argument checking is done by saving
the tree of the hidden length as formal->ts.cl->passed_length.
However, in the example above the a->ts.cl and b->ts.cl is
exactly the same. Thus only _b is saved as
formal->ts.cl->passed_length. Consequently, there will be two
checks for _b but none for _a. That way one misses bound
violations - and if optional arguments are involved (see PR)
one also gets false positives.
Solution: One needs to copy cl. One could do this relatively
early, but to reduce the number of backend declarations
(which are bad as the types are the same), I did it quite late
in trans-decl.c. One could guard it with a check for -fcheck=bounds
as it is only used with this flag.
Bootstrapped and regtested on x86-64-linux.
OK for the trunk?