[clang] [Clang] Implement P2280R4 Using unknown pointers and references in constant expressions (PR #95474)

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Thu May 1 10:38:24 PDT 2025


ahatanak wrote:

clang started issuing a warning after this PR was merged when compiling the following code:

```
typedef long unsigned int size_t;

extern "C" {
void *
  memcpy(void * __dst, const void * __src,
  size_t __n);
}

struct MyClass
{
    unsigned numberOfBuffers;
    unsigned long long buffers[1];
};
typedef struct MyClass MyClass;

MyClass a0;

void test() {
  const size_t byteSize = sizeof(MyClass) + sizeof(unsigned long long);
  MyClass &a1 = *(MyClass *)__builtin_alloca(byteSize);
  MyClass *a2 = (MyClass *)__builtin_alloca(byteSize);
  memcpy(&a1.buffers[0], &a0.buffers[0], sizeof(unsigned long long)); // no warnings.
  memcpy(&a1.buffers[1], &a0.buffers[0], sizeof(unsigned long long)); // warning: 'memcpy' will always overflow; destination buffer has size 0, but size argument is 8 [-Wfortify-source]
  memcpy(a2->buffers[1], &a0.buffers[0], sizeof(unsigned long long)); // no warnings.
}
```

Is the warning valid? Is the change in clang's behavior intentional?

https://github.com/llvm/llvm-project/pull/95474


More information about the cfe-commits mailing list