Bug 50790 - ICE in copy_constant on divide by expression that evaluates to zero
Summary: ICE in copy_constant on divide by expression that evaluates to zero
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2011-10-19 11:41 UTC by Jonathan Hogg
Modified: 2017-07-28 12:05 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-28 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Hogg 2011-10-19 11:41:08 UTC
The following code produces an ICE for me:

program test
   implicit none

   integer :: i

   i = 3

   print *, "chk", 3/(i-i)
end program test

This should presumably produce
   Error: Division by zero
at compile time (or run time if we're not being clever).

Produced with:
GNU Fortran (Debian 4.6.1-4) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.

Comment 1 Tobias Burnus 2011-10-19 15:23:55 UTC

      static integer(kind=4) C.1534 = 3 / 0;
      _gfortran_transfer_integer_write (&dt_parm.0, &C.1534, 4);

It gives with 4.7 the ICE:
  internal compiler error: in copy_constant, at varasm.c:3031

I think it is - at least in general - not be detectable in the front end but only in the ME.

A similar C test case works (cf. below for a simpler version); I think there is no compiler option to print a warning -- at least -Wall -Wextra don't.

main ()
  int i = 3;
  printf ("%d\n", 3/(i-i));
  return 0;
Comment 2 kargl 2011-10-19 17:54:31 UTC
Here's a variation on the original code, which removes
the print statement.  One does not need to wade through
gfortran IO structure.

program test
   implicit none
   integer i
   i = 3
   call foo (3 / (i - i))
end program test

subroutine foo(j)
  j = j * 1
end subroutine foo

The dump shows

foo (integer(kind=4) & restrict j)
  *j = NON_LVALUE_EXPR <*j>;

test ()
  integer(kind=4) i;

  i = 3;
    static integer(kind=4) C.1729 = 3 / 0;

    foo (&C.1729);

An equivalent C program compiles and gives a nearly identical
dump, but dies with a Floating exception(?)

troutmask:sgk[247] cat dw.c
foo (int *j)
  *j = *j * 2;
  static int i = 3;
  i = 3 / (i - i);
  return 0;
troutmask:sgk[248] ~/work/4x/bin/gcc -o z dw.c && ./z
Floating exception (core dumped)
Comment 3 Dominique d'Humieres 2017-07-28 12:05:47 UTC
The ICE has been fixed between revisions r250529 and r250610.