llvm / llvm/llvm-project

Analysis: Possible regression/False Positive in GCC 14/15 compared to Clang regarding -Wfree-nonheap-object with std::vector

Open
#192,582 4 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics question
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

### Hello LLVM team,
- I am encountering a confusing diagnostic issue while cross-testing my project with different compilers. I’ve noticed a persistent **-Wfree-nonheap-object** warning in GCC 14.2 and 15.2 (trunk) when compiling at -O3, whereas Clang 18/19 handles the same code perfectly without any warnings.
I suspect this might be a false positive in GCC's escape analysis during aggressive inlining, but I am having difficulty reporting this to the GCC team due to registration restrictions on their Bugzilla platform. I am sharing this here hoping to get some insights from the experts who work on compiler diagnostics and optimization, or perhaps someone who has access to GCC's tracker could help bridge this report.
### The Issue:
When using std::vector::reserve followed by multiple inlined push_back calls, GCC's analyzer seems to lose track of the vector's base pointer at -O3, incorrectly claiming that operator delete is called on a pointer with a non-zero offset.
### Comparison:
- Clang 18/19 (-O3 -std=c++20 -Wall): Clean. No warnings.
- GCC 14.2 / 15.2 (-O3 -std=c++20 -Wall): Throws warning: 'void operator delete(void*, std::size_t)' called on pointer '' with nonzero offset.
## Godbolt Link:
[GCC](https://godbolt.org/z/znhYz5Ghd)
[Clang](https://godbolt.org/z/5qzW8oGs5)

####
The code compiles successfully with GCC under both Debug mode and O2 optimization. I have not yet identified the underlying cause of this behavior, and I would greatly appreciate your assistance in helping me resolve this issue.
- Thank you for your time and for the amazing work on LLVM!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.