Bug 116534 - [14 regression] internal compiler error with comparison of pointers calculated with array offset
Summary: [14 regression] internal compiler error with comparison of pointers calculate...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P2 normal
Target Milestone: 14.3
Assignee: Marek Polacek
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2024-08-29 13:51 UTC by John Drouhard
Modified: 2024-10-17 13:46 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-08-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Drouhard 2024-08-29 13:51:47 UTC
The following produces an internal compiler when compiled with -Wall starting with gcc 14:


$ cat test.cpp
template <class A>
class Test {
    void foo(unsigned x, unsigned y) {
        bool test = &a[x] == &b[y];
    }
    unsigned *a;
    unsigned *b;
};



$ g++ -Wall ./test.cpp
./test.cpp: In member function 'void Test<A>::foo(unsigned int, unsigned int)':
./test.cpp:4:34: internal compiler error: Segmentation fault
    4 |         bool test = &a[x] == &b[y];
      |                                  ^
Please submit a full bug report, with preprocessed source (by using -freport-bug).
See <https://gcc.gnu.org/bugs/> for instructions.
Comment 1 Marek Polacek 2024-08-29 14:04:24 UTC
Started with r14-4793:

commit dad311874ac3b3cf4eca1c04f67cae80c953f7b8
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Oct 20 10:45:00 2023 -0400

    c++: remove NON_DEPENDENT_EXPR, part 1
Comment 2 GCC Commits 2024-09-17 21:03:39 UTC
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:7ca486889b1b1c7e7bcbbca3b6caa103294ec07d

commit r15-3673-g7ca486889b1b1c7e7bcbbca3b6caa103294ec07d
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Aug 29 10:40:50 2024 -0400

    c++: ICE with -Wtautological-compare in template [PR116534]
    
    Pre r14-4793, we'd call warn_tautological_cmp -> operand_equal_p
    with operands wrapped in NON_DEPENDENT_EXPR, which works, since
    o_e_p bails for codes it doesn't know.  But now we pass operands
    not encapsulated in NON_DEPENDENT_EXPR, and crash, because the
    template tree for &a[x] has null DECL_FIELD_OFFSET.
    
    This patch extends r12-7797 to cover the case when DECL_FIELD_OFFSET
    is null.
    
            PR c++/116534
    
    gcc/ChangeLog:
    
            * fold-const.cc (operand_compare::operand_equal_p): If either
            field's DECL_FIELD_OFFSET is null, compare the fields with ==.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/warn/Wtautological-compare4.C: New test.
    
    Reviewed-by: Jason Merrill <jason@redhat.com>
Comment 3 Marek Polacek 2024-09-17 21:05:43 UTC
Fixed on trunk so far.
Comment 4 GCC Commits 2024-10-17 13:43:27 UTC
The releases/gcc-14 branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:36de56d3d643e7b8131fa6671b9e85258a1d1ca1

commit r14-10800-g36de56d3d643e7b8131fa6671b9e85258a1d1ca1
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Aug 29 10:40:50 2024 -0400

    c++: ICE with -Wtautological-compare in template [PR116534]
    
    Pre r14-4793, we'd call warn_tautological_cmp -> operand_equal_p
    with operands wrapped in NON_DEPENDENT_EXPR, which works, since
    o_e_p bails for codes it doesn't know.  But now we pass operands
    not encapsulated in NON_DEPENDENT_EXPR, and crash, because the
    template tree for &a[x] has null DECL_FIELD_OFFSET.
    
    This patch extends r12-7797 to cover the case when DECL_FIELD_OFFSET
    is null.
    
            PR c++/116534
    
    gcc/ChangeLog:
    
            * fold-const.cc (operand_compare::operand_equal_p): If either
            field's DECL_FIELD_OFFSET is null, compare the fields with ==.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/warn/Wtautological-compare4.C: New test.
    
    Reviewed-by: Jason Merrill <jason@redhat.com>
    (cherry picked from commit 7ca486889b1b1c7e7bcbbca3b6caa103294ec07d)
Comment 5 Marek Polacek 2024-10-17 13:46:14 UTC
Fixed.