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

            Bug ID: 125496
           Summary: [17 regression] C++23 ASAN reports
                    new-delete-type-mismatch for std::vector since
                    r17-603-gacfdad706d8
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: enrico.seiler+gccbugs at outlook dot com
  Target Milestone: ---

With `-std=c++23 -fsanitize=address`.
Since r17-603-gacfdad706d8.

```
#include <tuple>
#include <vector>

int main()
{
    std::vector<std::tuple<int,int,int>> column(1);
    return 0;
}
```

We need 3*4 = 12 bytes for the tuple.
C++23's new_allocator uses `allocate_at_least`, which then rounds up to 16.
But for `deallocate`, it still uses `n * sizeof(_Tp)`, i.e. 3 * 4 = 12.

```
=================================================================
==1==ERROR: AddressSanitizer: new-delete-type-mismatch on 0x759c00be0010 in
thread T0:
  object passed to delete has wrong type:
  size of the allocated type:   16 bytes;
  size of the deallocated type: 12 bytes.
    #0 0x797c01fae13f in operator delete(void*, unsigned long)
(/opt/compiler-explorer/gcc-snapshot/lib64/libasan.so.8+0x12e13f) (BuildId:
9587614c4d1a529cf584930cee595020285e2b52)
    #1 0x00000040214a in std::__new_allocator<std::tuple<int, int, int>
>::deallocate(std::tuple<int, int, int>*, unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/new_allocator.h:222
    #2 0x000000401b4b in std::allocator<std::tuple<int, int, int>
>::deallocate(std::tuple<int, int, int>*, unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/allocator.h:238
    #3 0x000000401b4b in std::allocator_traits<std::allocator<std::tuple<int,
int, int> > >::deallocate(std::allocator<std::tuple<int, int, int> >&,
std::tuple<int, int, int>*, unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/alloc_traits.h:688
    #4 0x000000401b4b in std::_Vector_base<std::tuple<int, int, int>,
std::allocator<std::tuple<int, int, int> > >::_M_deallocate(std::tuple<int,
int, int>*, unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/stl_vector.h:448
    #5 0x00000040171a in std::_Vector_base<std::tuple<int, int, int>,
std::allocator<std::tuple<int, int, int> > >::~_Vector_base()
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/stl_vector.h:391
    #6 0x0000004014c3 in std::vector<std::tuple<int, int, int>,
std::allocator<std::tuple<int, int, int> > >::~vector()
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/stl_vector.h:847
    #7 0x00000040130c in main /app/example.cpp:8
    #8 0x797c01829d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId:
095c7ba148aeca81668091f718047078d57efddb)
    #9 0x797c01829e3f in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId:
095c7ba148aeca81668091f718047078d57efddb)
    #10 0x000000401174 in _start (/app/output.s+0x401174) (BuildId:
316d243a4630e55b999ddabc9f8536c1a49648c7)

0x759c00be0010 is located 0 bytes inside of 16-byte region
[0x759c00be0010,0x759c00be0020)
allocated by thread T0 here:
    #0 0x797c01fad27f in operator new(unsigned long)
(/opt/compiler-explorer/gcc-snapshot/lib64/libasan.so.8+0x12d27f) (BuildId:
9587614c4d1a529cf584930cee595020285e2b52)
    #1 0x000000402321 in std::__new_allocator<std::tuple<int, int, int>
>::allocate_at_least(unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/new_allocator.h:192
    #2 0x000000401d70 in std::allocator<std::tuple<int, int, int>
>::allocate_at_least(unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/allocator.h:221
    #3 0x000000401d70 in std::allocator_traits<std::allocator<std::tuple<int,
int, int> > >::allocate_at_least(std::allocator<std::tuple<int, int, int> >&,
unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/alloc_traits.h:674
    #4 0x000000401d70 in std::_Vector_base<std::tuple<int, int, int>,
std::allocator<std::tuple<int, int, int> > >::_M_allocate_at_least(unsigned
long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/stl_vector.h:418
    #5 0x0000004019f8 in std::_Vector_base<std::tuple<int, int, int>,
std::allocator<std::tuple<int, int, int> > >::_M_create_storage(unsigned long)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/stl_vector.h:457
    #6 0x000000401638 in std::_Vector_base<std::tuple<int, int, int>,
std::allocator<std::tuple<int, int, int> > >::_Vector_base(unsigned long,
std::allocator<std::tuple<int, int, int> > const&)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/stl_vector.h:354
    #7 0x0000004013f2 in std::vector<std::tuple<int, int, int>,
std::allocator<std::tuple<int, int, int> > >::vector(unsigned long,
std::allocator<std::tuple<int, int, int> > const&)
/cefs/94/94924b5b7114d9d2c868124b_gcc-trunk-20260529/include/c++/17.0.0/bits/stl_vector.h:629
    #8 0x0000004012e8 in main /app/example.cpp:6
    #9 0x797c01829d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId:
095c7ba148aeca81668091f718047078d57efddb)

SUMMARY: AddressSanitizer: new-delete-type-mismatch /app/example.cpp:8 in main
==1==HINT: if you don't care about these errors you may set
ASAN_OPTIONS=new_delete_type_mismatch=0
==1==ABORTING
```

Reply via email to