Page MenuHomeFreeBSD

libcasper: Neuter false positive -Wuse-after-free warnings from GCC 13
ClosedPublic

Authored by jhb on Nov 13 2023, 10:29 PM.
Tags
None
Referenced Files
F86369275: D42576.diff
Wed, Jun 19, 12:42 PM
Unknown Object (File)
Sat, May 25, 8:35 AM
Unknown Object (File)
Sat, May 25, 8:35 AM
Unknown Object (File)
Sat, May 25, 8:31 AM
Unknown Object (File)
Sat, May 25, 8:16 AM
Unknown Object (File)
Mar 13 2024, 12:41 PM
Unknown Object (File)
Mar 10 2024, 6:42 AM
Unknown Object (File)
Mar 10 2024, 6:42 AM
Subscribers

Details

Summary

GCC 13 incorrectly thinks a call to free after a failed realloc is a
use after free.

lib/libcasper/services/cap_grp/cap_grp.c: In function 'group_resize':
lib/libcasper/services/cap_grp/cap_grp.c:65:17: error: pointer 'buf' may be used after 'realloc' [-Werror=use-after-free]

65 |                 free(buf);
   |                 ^~~~~~~~~

lib/libcasper/services/cap_grp/cap_grp.c:63:19: note: call to 'realloc' here

63 |         gbuffer = realloc(buf, gbufsize);
   |                   ^~~~~~~~~~~~~~~~~~~~~~

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

jhb requested review of this revision.Nov 13 2023, 10:29 PM
This revision is now accepted and ready to land.Nov 13 2023, 11:16 PM

I thought this is becoming UB behavior...
But for our implementation it's well defined.

In D42576#971550, @imp wrote:

I thought this is becoming UB behavior...
But for our implementation it's well defined.

If realloc fails and returns NULL, the original pointer is always valid, that's how realloc is defined in C. There is some discussion about making realloc() with a new size of 0 as UB to permit varying implementations, but that isn't relevant here.

In D42576#971578, @jhb wrote:
In D42576#971550, @imp wrote:

I thought this is becoming UB behavior...
But for our implementation it's well defined.

If realloc fails and returns NULL, the original pointer is always valid, that's how realloc is defined in C. There is some discussion about making realloc() with a new size of 0 as UB to permit varying implementations, but that isn't relevant here.

realloc(p, 0) is slated to be undefined behavior in C23. You are correct... I thought wrong. This all looks good.