Bug 106698

Summary: bogus -Wstringop-overflow warning on Ada code with LTO
Product: gcc Reporter: Jesper Quorning <jesper.quorning>
Component: tree-optimizationAssignee: Eric Botcazou <ebotcazou>
Status: RESOLVED FIXED    
Severity: normal CC: ebotcazou
Priority: P3 Keywords: diagnostic
Version: 11.2.0   
Target Milestone: 13.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2022-10-05 00:00:00
Bug Depends on:    
Bug Blocks: 88443    
Attachments: Minimal reproducer

Description Jesper Quorning 2022-08-20 17:42:12 UTC
Created attachment 53484 [details]
Minimal reproducer

Building `program.ada` with optimisation -O2 or above:

% gnatmake -O2 -flto main.adb

Causes linker warning:
```
gcc -c -O2 -flto main.adb
gcc -c -O2 -flto test.adb
gnatbind -x main.ali
gnatlink main.ali -O2 -flto
test.adb: In function 'test__assign':
test.adb:6:21: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
    6 |       The_Array (1) := AAAAA_Enum;
      |                     ^
test.ads:11:4: note: at offset -9223372036854775808 into destination object 'F' of size 4
   11 |    The_Array : T_Array;
      |    ^
```

The program is essentially:
   type First_Enum is (AAAAA_Enum);
   subtype Second_Enum is First_Enum;
   type T_Array is array (Integer range -1 .. 2) of Second_Enum;
   -- Change 'First to something >= 0 and the warning disappears.
   -- Using the First_Enum instead of Second_Enum and the warning disappears.

GNATMAKE 12.1.0 - cause warning
GNATMAKE 11.2.0 - cause warning
GNATMAKE 10.3.0 - cause no warning
Comment 1 Andrew Pinski 2022-08-20 23:44:42 UTC
I suspect the diagnostic code does not handle negative lower bounds arrays correctly.
Comment 2 Eric Botcazou 2022-10-05 10:12:37 UTC
> I suspect the diagnostic code does not handle negative lower bounds arrays
> correctly.

Yes, see https://gcc.gnu.org/pipermail/gcc-patches/2022-August/599820.html
Comment 3 GCC Commits 2022-10-05 11:49:29 UTC
The master branch has been updated by Eric Botcazou <ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:853ce8eea4ff97850a987167e603387b3d0f1401

commit r13-3084-g853ce8eea4ff97850a987167e603387b3d0f1401
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Wed Oct 5 12:21:03 2022 +0200

    Fix bogus -Wstringop-overflow warning in Ada
    
    It comes from a discrepancy between get_offset_range, which uses a signed
    type, and handle_array_ref, which uses an unsigned one, to do computations.
    
    gcc/
            PR tree-optimization/106698
            * pointer-query.cc (handle_array_ref): Fix handling of low bound.
    
    gcc/testsuite/
            * gnat.dg/lto26.adb: New test.
            * gnat.dg/lto26_pkg1.ads, gnat.dg/lto26_pkg1.adb: New helper.
            * gnat.dg/lto26_pkg2.ads, gnat.dg/lto26_pkg2.adb: Likewise.
Comment 4 Eric Botcazou 2022-10-05 11:54:51 UTC
Fixed on the mainline.