Bug 46182 - Run time check for invalid use of unallocated allocatable variables
Summary: Run time check for invalid use of unallocated allocatable variables
Status: RESOLVED DUPLICATE of bug 20520
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.6.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-26 12:17 UTC by Dominique d'Humieres
Modified: 2018-05-03 14:00 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-10-26 14:29:15


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dominique d'Humieres 2010-10-26 12:17:06 UTC
While the following code

type b
  integer, allocatable :: i(:)
end type b
type(b), allocatable :: a(:)
!allocate(a(10))
print *, allocated(a)!, allocated(a(1)%i)
!deallocate(a)
end

seems to be a valid f2003 code and gives 'F' at run time, replacing

print *, allocated(a)!, allocated(a(1)%i)

with

print *, allocated(a), allocated(a(1)%i)

gives a "Segmentation fault" at run time (it gives 'T F' if the allocate/deallocate lines are uncommented).

When asked on IRC about this kind of inquiry of a component of an unallocated derived type, Tobias Burnus answered immediately that such a use is invalid, pointing to

2.4.9 Allocatable variables
...
2 An unallocated allocatable variable shall not be referenced or defined.
...

Then he made the comment that (obviously) this restriction does not apply for

13.7.11 ALLOCATED (ARRAY) or ALLOCATED (SCALAR)
1 Description. Query allocation status.
2 Class. Inquiry function.
3 Arguments.
   ARRAY shall be an allocatable array.
  SCALAR shall be an allocatable scalar.
4 Result Characteristics. Default logical scalar.
5 Result Value. The result has the value true if the argument (ARRAY or SCALAR) is allocated and has the
value false if the argument is unallocated.

but that 'a(1)%i' is not an array but an invalid expression per 2.4.9 that cannot be a valid argument of allocated. Note that my original problem came from scalar allocatable and that the above was less obvious for 'a%i'.

It would be nice to have a run time check for such invalid use of unallocated allocatable variables (such as -fcheck=use_unalloc).
Comment 1 Dominique d'Humieres 2010-10-26 14:06:55 UTC
Forwarded from http://gcc.gnu.org/ml/gcc-bugs/2010-10/msg02167.html
> > It would be nice to have a run time check for such invalid use of
> > unallocated allocatable variables (such as -fcheck=use_unalloc).
> If you use an unallocated variable you get a segmentation fault. 
> Isn't this a sufficient runtime check ?

Well! You can say the same thing for -fcheck=bounds if writing outside array  bounds gives you a segmentation fault! Nevertheless nobody will argue that this check is useless: it can save you hours of debugging to locate the line(s) in which you do it.

Segmentation faults tell you that there is something wrong, but not why and/or where (not counting gfortran bugs). It took me some time to understand it for a less than 20 line code in which I had the bad idea of using a copy and paste from a valid line to an invalid one (this can happen to anybody with kloc!-)
Comment 2 Mikael Morin 2010-10-26 14:29:15 UTC
(In reply to comment #1)
> Well! You can say the same thing for -fcheck=bounds if writing outside array 
> bounds gives you a segmentation fault! Nevertheless nobody will argue that this
> check is useless: it can save you hours of debugging to locate the line(s) in
> which you do it.
Well, my point was that an out of bound access doesn't necessarily faults whereas a NULL pointer access always does. 
Anyway, it can't do any harm. Confirmed.
Comment 3 Dominique d'Humieres 2018-01-01 15:12:10 UTC
Another variant of pr20520.

*** This bug has been marked as a duplicate of bug 20520 ***