[compiler-rt] [compiler-rt]Return size 0 explicitly when both pointers are NULL (PR #81789)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 14 13:07:10 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-pgo

Author: Mingming Liu (minglotus-6)

<details>
<summary>Changes</summary>

* Subtracting two NULL pointers is [undefined behavior](https://stackoverflow.com/questions/55747642/can-we-subtract-null-pointers#:~:text=Subtracting%20two%20NULL%20pointers%20is,of%20the%20two%20array%20elements.) in C standard. Converting NULL to `intptr_t` and subtracting two `intptr_t` is also implementation specific behavior. (This [SO](https://stackoverflow.com/questions/64739189/three-questions-is-null-null-defined-is-uintptr-tnull-uintptr-tnull-de?noredirect=1&lq=1) discusses a similar question)
* On the other hand, it's defined behavior to add or subtract NULL from [since C++03](https://stackoverflow.com/questions/8128168/is-the-behavior-of-subtracting-two-null-pointers-defined)

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


1 Files Affected:

- (modified) compiler-rt/lib/profile/InstrProfilingBuffer.c (+4) 


``````````diff
diff --git a/compiler-rt/lib/profile/InstrProfilingBuffer.c b/compiler-rt/lib/profile/InstrProfilingBuffer.c
index af52804b2b532c..faaa9dfb55ada3 100644
--- a/compiler-rt/lib/profile/InstrProfilingBuffer.c
+++ b/compiler-rt/lib/profile/InstrProfilingBuffer.c
@@ -93,11 +93,15 @@ uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End) {
 COMPILER_RT_VISIBILITY
 uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,
                                              const char *End) {
+  if (Begin == NULL && End == NULL)
+    return 0;
   return (End - Begin);
 }
 
 COMPILER_RT_VISIBILITY
 uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End) {
+  if (Begin == NULL && End == NULL)
+    return 0;
   return End - Begin;
 }
 

``````````

</details>


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


More information about the llvm-commits mailing list