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
          Reporter: willwalker26 at gmail dot com
  Target Milestone: ---

Created attachment 41961
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'

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];

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,

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

Reply via email to