[Patch,Fortran] RFC - PR42958 - cleanup temporary var malloc

Tobias Burnus burnus@net-b.de
Wed Feb 17 07:20:00 GMT 2010


Hi all,

currently, for temporary arrays the following happens:

(1)  bounds = (ubound - lbound + 1)
(2)  size = bounds < 0 ? 0 : bounds * 8
(2) gfc_call_malloc (...., size)

where "8" in this example is the byte size.

gfc_call_malloc does:

(a) if (size < 0) error "Negative Memory"
(b) ptr =  malloc ( MAX(1, size))
(c) if (ptr == NULL) error "Could not allocate"

The overflow check at (a) does not work reliable - while for 32bit there
is the chance that it can find problems (i.e. for arrays which are
slightly too large), for 64bit it is rather unlikely to be useful. The
patch removes this overflow check.

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

 * * *

Note 1: One should consider to make the error check (c) optional;
though, the question is on which option it should depend -- on -fcheck=
or -O<n> or ...?
Maybe   if (flag.O == 0 || (flag.fcheck & <something>)), though the
question is what "something" should be.

Note 2: The "MAX (1, size)" is needed to make "allocated
(zero_sized_array)" possible; it will be obsoleted by the array
descriptor patch; the allocation would be then:

if (size > 0) {
  desc.ptr = malloc (size)
  if (desc.ptr == NULL) error "could not allocate"
}
desc.allocated = true
-- or with stat= --
desc.allocated = true
if (size > 0) {
  desc.ptr = malloc (size)
  if (desc.ptr == NULL) { desc.stat = error_status; desc.allocated = false }
}

Tobias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: malloc.diff
Type: text/x-patch
Size: 2367 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20100217/19c0f8ed/attachment.bin>


More information about the Gcc-patches mailing list