[Bug c/81785] New: Segmentation fault for signed overflow in index expression when -fwrapv is enabled

willwalker26 at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Aug 9 18:41:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81785

            Bug ID: 81785
           Summary: Segmentation fault for signed overflow in index
                    expression when -fwrapv is enabled
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: willwalker26 at gmail dot com
  Target Milestone: ---

Created attachment 41961
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41961&action=edit
reproduction preprocessed file

gcc version 4.9.2 (Debian 4.9.2-10)
Target: x86_64-linux-gnu
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O0' '-fwrapv' '-mtune=generic'
'-march=x86-64'

Compiling the c file (see the attached preprocessed file):

% gcc -O0 -fwrapv repro.c

Execution results in:

% ./a.out
Segmentation fault

I also tried with varied levels of optimization to the same results. It looks
like 4.7.x and earlier versions of gcc work as expected.

The offending code segment is:

 unsigned int k;
 unsigned int i;
 i = 0U;
 k = 2147483648U;
 while (k < 2147483658U) {
  y[i] = x[(int)k - (int)2147483611];
  k++;
  i++;
 }

What I was hoping to happen, is to have (int)k wrap to some large negative
value and the following signed subtraction wrap to a small positive value in
the range [37,46]. Note the arrays for x and y are length 100.

I am aware that signed overflow is not defined in C, but was hoping that using
the -fwrapv compiler option would let this through.

Note, hoisting out the index expression results in the expected execution,
e.g.:

tmp = (int)k - (int)2147483611;
y[i] = x[tmp];


More information about the Gcc-bugs mailing list