Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtsan][NFC] Put in comment describing why freeing a nullptr is safe #113720

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
Collaborator

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

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.

2 participants