https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120744
Bug ID: 120744
Summary: New diagnostic: -Wrealloc-zero-bytes
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
Hi!
I'm working on a standards proposal for fixing realloc(p,0) so that it can be
used portably. The only regression that I expect it would cause are minor
leaks of 0 bytes plus metadata in the case where one calls realloc(p,0)
ignoring the return value as a synonym of free(p).
This could be addressed by adding a diagnostic for realloc(3) calls where the
second argument is a constant expression with the value 0. The diagnostic
should of course also warn for reallocarray(3) calls where either the second or
the third (or both) arguments are a constant expression with the value 0.
See
<https://inbox.sourceware.org/libc-alpha/xdt4l76ircncs6kh5ikhdsn5hsmh3wwceecl2pzg6zmqr7i2sm@slquforl5gfu/T/#m2a94ded87ff8a06a2384705867983f2df72b172b>.
So, please add a diagnostic for those cases, so that all existing code that
uses realloc(p,0) with a hard-coded 0 can be replaced by free(p).
This diagnostic should be at the very least in -Wextra (possibly -Wall), as we
want to catch all existing code using this pattern.