[compiler-rt] [rtsan][NFC] Put in comment describing why freeing a nullptr is safe (PR #113720)
Chris Apple via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 25 11:21:52 PDT 2024
https://github.com/cjappl created https://github.com/llvm/llvm-project/pull/113720
Just documenting this for future devs.
Also moved to `nullptr` and deleted unnecessary braces as per the coding standard.
A few sources:
https://en.cppreference.com/w/c/memory/free (search for "function does nothing")
https://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf
> The free function causes the space pointed to by ptr to be deallocated, that is, made
available for further allocation. If ptr is a null pointer, no action occurs
https://github.com/bminor/glibc/blob/c5dd659f22058bf9b371ab1cba07631f1206c674/malloc/malloc.c#L3363-L3364
>From 81f4587dffd8f4ee6785e070d20de8709fe6361a Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Fri, 25 Oct 2024 11:19:26 -0700
Subject: [PATCH] [rtsan][NFC] Put in comment describing why freeing a nullptr
is safe
---
compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 890d6c11c40762..a65871b17da5a8 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -431,9 +431,12 @@ INTERCEPTOR(void, free, void *ptr) {
if (DlsymAlloc::PointerIsMine(ptr))
return DlsymAlloc::Free(ptr);
- if (ptr != NULL) {
+ // According to the C and C++ standard, freeing a nullptr is guaranteed to be
+ // a no-op (and thus real-time safe). This can be confirmed for looking at
+ // __libc_free in the glibc source.
+ if (ptr != nullptr)
__rtsan_notify_intercepted_call("free");
- }
+
return REAL(free)(ptr);
}
More information about the llvm-commits
mailing list