Bug 119426 - undefined increment of deallocated pointer allowed in constexpr
Summary: undefined increment of deallocated pointer allowed in constexpr
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2025-03-22 14:34 UTC by Jonathan Wakely
Modified: 2025-03-23 08:29 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-03-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2025-03-22 14:34:00 UTC

    
Comment 1 Jonathan Wakely 2025-03-22 14:36:06 UTC
This should be rejected, but all of gcc, clang, msvc and EDG are totally cool with it:

constexpr bool
test2()
{
  int* p = new int[2];
  int* x = p;
  delete[] p;
  p = new int[4];
  void* v = nullptr;
  for (int i = 0; i < 2; ++i)
    v = x++;
  delete[] p;
  return v != p;
}

static_assert( test2() );


The other compilers give an error if x is increment more than twice, but GCC let's you keep incrementing it.
Comment 2 Jonathan Wakely 2025-03-22 14:37:38 UTC
https://godbolt.org/z/f1jxvMvdz
Comment 3 Jonathan Wakely 2025-03-22 14:41:34 UTC
Similar to Bug 111242 but here the pointer has already been deallocated.