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

            Bug ID: 100307
           Summary: Wrong placement-new warning
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: s.rueckerl at tum dot de
  Target Milestone: ---

A placement new warning is generated whenever the placement new is used with
in-line pointer arithmetic resulting in a negative offset to the original
pointers address. It does not fail with a positive offset or if the calculation
is done with a temporary variable before.

This did not happen with GCC-10 and can be observed with different GCC-11
versions (and the trunk version as provided on godbolt.org).

The following code replicates this behavior: (https://godbolt.org/z/nPGvEM44e)

```
#include <new>

static char myMemory [128];

int main()
{
    //make it fail
    char* memoryPtr = myMemory+32;
    int* myValue = new (memoryPtr - 1) int {42};

    //does not fail with positive offset
    char* secondMemoryPtr = myMemory+64;
    int* mySecondValue = new (secondMemoryPtr + 1) int {42};

    //does not fail with temporary
    char* thirdMemoryPtr = myMemory+96;
    char* placementNewAddress = thirdMemoryPtr - 1;
    int* myThirdValue = new (placementNewAddress) int {1};

    // use all variables for return value to avoid unused variable warnings
    return *myValue + *mySecondValue + *myThirdValue;
}
```

Reply via email to