Skip to content

Conversation

@cjappl
Copy link
Contributor

@cjappl cjappl commented Oct 25, 2024

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

@llvmbot
Copy link
Member

llvmbot commented Oct 25, 2024

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

Author: Chris Apple (cjappl)

Changes

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

@cjappl cjappl merged commit dbece8e into llvm:main Oct 31, 2024
10 checks passed
@cjappl cjappl deleted the free_comment branch October 31, 2024 00:39
smallp-o-p pushed a commit to smallp-o-p/llvm-project that referenced this pull request Nov 3, 2024
…lvm#113720)

Just documenting this for future devs.

Also moved to `nullptr` and deleted unnecessary braces as per the coding
standard.
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…lvm#113720)

Just documenting this for future devs.

Also moved to `nullptr` and deleted unnecessary braces as per the coding
standard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants