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

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 14 13:05:32 PST 2024


https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/81789

* 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)

>From 5513047493925e5ca08c31a0f5b17250e079fe4f Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Wed, 14 Feb 2024 12:54:10 -0800
Subject: [PATCH] [compiler-rt]Return size 0 when both pointers are NULL

---
 compiler-rt/lib/profile/InstrProfilingBuffer.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/compiler-rt/lib/profile/InstrProfilingBuffer.c b/compiler-rt/lib/profile/InstrProfilingBuffer.c
index af52804b2b532c..50da9124259e00 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;
 }
 



More information about the llvm-commits mailing list