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

            Bug ID: 123271
           Summary: -ftrapv fails to detect overflow on ARM32 since GCC 10
           Product: gcc
           Version: 14.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jiannmao at qq dot com
  Target Milestone: ---

Starting from GCC 10, the -ftrapv flag no longer works correctly on ARM32
architecture. Programs compiled with -ftrapv fail to abort on signed integer
overflow and instead produce incorrect overflow results.

Target: ARM32 (tested on arm-linux-gnueabi, armv7-a)
Command: gcc -ftrapv test.c -o test

```
long sub(long a, long b)
{
        return a - b;
}

int main(void)
{
        printf("%x\n", sub(0x80000000, 10));  // should overflow and abort, but
get incorrectly prints: 7ffffff6

        return 0;
}

```
The program should abort when overflow occurs (as it did in GCC 9 and
earlier).but it completes normally and returns an incorrect overflowed result:
0x7ffffff6


GCC 9 (CORRECT - calls __subvsi3):
```
sub:
        push    {r7, lr}
        sub     sp, sp, #8
        add     r7, sp, #0
        str     r0, [r7, #4]
        str     r1, [r7]
        ldr     r1, [r7]
        ldr     r0, [r7, #4]
        bl      __subvsi3          # Calls libgcc function that checks overflow
        mov     r3, r0
        mov     r0, r3
        adds    r7, r7, #8
        mov     sp, r7
        pop     {r7, pc}
```

GCC 10+ (INCORRECT - direct subtraction without checking):
```
sub:
        push    {r7}
        sub     sp, sp, #12
        add     r7, sp, #0
        str     r0, [r7, #4]
        str     r1, [r7]
        ldr     r2, [r7, #4]
        ldr     r3, [r7]
        subs    r3, r2, r3
        mov     r0, r3               # No overflow check after subs
        adds    r7, r7, #12
        mov     sp, r7
        ldr     r7, [sp], #4
        bx      lr
```

Reply via email to