[compiler-rt] [rtsan][NFC] Put in comment describing why freeing a nullptr is safe (PR #113720)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 11:22:28 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Chris Apple (cjappl)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/113720.diff


1 Files Affected:

- (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+5-2) 


``````````diff
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);
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/113720


More information about the llvm-commits mailing list