public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dfranke at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/30720]  New: runtime: check for empty array slices before allocating a negative amount of memory
Date: Tue, 06 Feb 2007 20:08:00 -0000	[thread overview]
Message-ID: <bug-30720-13648@http.gcc.gnu.org/bugzilla/> (raw)

The program below terminates with a runtime error due to an attempt to allocate
a negative amount of memory. The error occurs while allocating memory for a
temporary, empty, array slice. 

$> cat pr.f90
program runtime_error
  REAL    :: a(5), b
  INTEGER :: l, u
  l = 4
  u = 2

  a = (/ 1.0, 2.0, 3.0, 4.0, 5.0 /)
  b = f(a(l:u) - 3.0)

  CONTAINS
    REAL FUNCTION f(x)
      REAL, DIMENSION(:), INTENT(in) :: x
      f = sum(x)
    end function
END PROGRAM


There are two issues here:
 a) the runtime error as described above

$> gfortran-svn -O -fdump-tree-original -fdump-tree-optimized pr.f90

from dump-tree-original:
      int4 D.1036;
      void * D.1035;
      int4 D.1034;
      struct array1_real4 atmp.7;
      int4 D.1032;
      int4 D.1031;

      D.1031 = l;
      D.1032 = u;
      atmp.7.dtype = 281;
      atmp.7.dim[0].stride = 1;
      atmp.7.dim[0].lbound = 0;
      atmp.7.dim[0].ubound = u - D.1031;
      D.1034 = (u - D.1031) + 1;
      D.1035 = _gfortran_internal_malloc (D.1034 * 4);
      atmp.7.data = D.1035;

from dump-tree-optimized:
  void * SR.31;
  [...]
  SR.31 = _gfortran_internal_malloc (-4);
  _gfortran_internal_free (SR.31);


Here, if l > u+1, D.1035 will be negative and trigger the runtime error.
In this case, a runtime check whether the slice is empty before calling
_gfortran_internal_malloc() would result in code not stopping with a
(seemingly) unrelated error message.


 b) missed optimization during constant folding:
from dump-tree-original:
      struct array1_real4 atmp.7;

      atmp.7.dtype = 281;
      atmp.7.dim[0].stride = 1;
      atmp.7.dim[0].lbound = 0;
      atmp.7.dim[0].ubound = -1;
      atmp.7.data = 0B;
      [...]
      _gfortran_internal_free (atmp.7.data);

from dump-tree-optimzed:
  _gfortran_internal_free (0B);

If the boundaries are known at compile time, e.g. a(4:2), no memory is
allocated for the array descriptor, but _gfortran_internal_free(0B) is called
nonetheless. This call is still visible after optimization.

$> gfortran-svn -v
gcc version 4.3.0 20070126 (experimental)


-- 
           Summary: runtime: check for empty array slices before allocating
                    a negative amount of memory
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dfranke at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30720


             reply	other threads:[~2007-02-06 20:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-06 20:08 dfranke at gcc dot gnu dot org [this message]
2007-02-06 20:34 ` [Bug fortran/30720] " fxcoudert at gcc dot gnu dot org
2007-02-06 21:00 ` fxcoudert at gcc dot gnu dot org
2007-02-06 23:13 ` fxcoudert at gcc dot gnu dot org
2007-02-06 23:16 ` fxcoudert at gcc dot gnu dot org
2007-02-07 17:53 ` tkoenig at gcc dot gnu dot org
2007-02-07 19:10 ` fxcoudert at gcc dot gnu dot org
2007-02-07 20:49 ` tkoenig at gcc dot gnu dot org
2007-02-09 20:31 ` fxcoudert at gcc dot gnu dot org
2007-02-09 20:33 ` [Bug fortran/30720] [4.2/4.1 only] " fxcoudert at gcc dot gnu dot org
2007-02-16 12:20 ` fxcoudert at gcc dot gnu dot org
2007-02-16 15:55 ` [Bug fortran/30720] [4.1 " fxcoudert at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-30720-13648@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).