This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug fortran/49479] [4.6/4.7 Regression] reshape / optionals / zero sized arrays
- From: "burnus at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 21 Jun 2011 08:58:50 +0000
- Subject: [Bug fortran/49479] [4.6/4.7 Regression] reshape / optionals / zero sized arrays
- Auto-submitted: auto-generated
- References: <bug-49479-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49479
--- Comment #4 from Tobias Burnus <burnus at gcc dot gnu.org> 2011-06-21 08:58:31 UTC ---
(In reply to comment #3)
> I think gfortran's convention is - and also TR 29113 requires - that the
> addr_expr is not NULL if the variable is allocated - even if it is zero sized.
The TR 29113 draft (Interop ML version) has (5.2.3 CFI_cdesc_t)
ftp://ftp.nag.co.uk/sc22wg5/N1851-N1900/N1854.pdf
"void * base addr; If the object is an unallocated allocatable or a pointer
that is disassociated, the value is NULL. If the object has zero size, the
value is not NULL but is otherwise processor-dependent. Otherwise [...]"
And trans.c has for gfc_call_malloc:
/* Call malloc to allocate size bytes of memory, with special conditions:
+ if size == 0, return a malloced area of size 1,
If one looks at RESHAPE in the library:
http://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libgfortran/intrinsics/reshape_generic.c;hb=HEAD
one sees:
86 if (ret->data == NULL)
87 {
88 rs = 1;
89 for (n = 0; n < rdim; n++)
90 {
91 rex = shape_data[n];
93 GFC_DIMENSION_SET(ret->dim[n],0,rex - 1,rs);
95 rs *= rex;
96 }
98 ret->data = internal_malloc_size ( rs * size );
Thus, if shape_data[n] == 0 -- which is here the case --, "rs" == 0 and thus
one calls malloc with zero size. POSIX states:
"If size is 0, either a null pointer or a unique pointer that can be
successfully passed to free() shall be returned."
Hence, the "rs*size" should be replaced by "MAX(1, rs*size)".
I think other intrinsics will have the same problem.