On Fri, 17 Feb 2023, 03:49 Siddhesh Poyarekar, wrote: > On 2023-02-16 10:15, David Malcolm via Gcc wrote: > > I'm not convinced that it's useful to the end-user to warn about the > > "use of q itself" case. > > FWIW, -Wuse-after-free=3 already should do this: > > At level 3, the warning also diagnoses uses of indeterminate pointers in > equality expressions. All uses of indeterminate pointers are undefined > but equality tests sometimes appear after > calls to "realloc" as an attempt to determine whether the call resulted > in relocating the object to a different address. They are diagnosed at > a separate level to aid legacy code gradually > transition to safe alternatives. For example, the equality test in the > function below is diagnosed at this level: > > Jakub and I had discussed this in the context of _FORTIFY_SOURCE=3 > (which is anal about this and can break things) and we got pr#105217, > but that is also a best-effort thing, not really a guarantee. > > IMO the analyzer should go that extra mile and warn for the use of q > itself and maybe deprecate -Wuse-after-free=3 in its favour. > Please be aware that in C++ it's implementation-defined, not undefined. That means that an implementation without trap representations for pointers can choose to make it behave just like using (uintptr_t)p. https://cplusplus.github.io/CWG/issues/1438.html https://cplusplus.github.io/CWG/issues/623.html https://cplusplus.github.io/CWG/issues/616.html https://cplusplus.github.io/CWG/issues/312.html We could still warn in C++ (because the code isn't portable) but I would strongly suggest we don't influence C++ codegen based on deallocated pointers being undefined. I don't think gcc supports any targets with trapping pointers, and there are quite enough sources of UB already. We don't need to create traps for users where there are no traps for pointers :-)