[libc-commits] [libc] 8628ca2 - [libc] Fix UB in memory utils (#74295)

via libc-commits libc-commits at lists.llvm.org
Mon Dec 4 01:57:39 PST 2023


Author: Guillaume Chatelet
Date: 2023-12-04T10:57:35+01:00
New Revision: 8628ca29aa4714f99e865c99b9d510ad14897fdc

URL: https://github.com/llvm/llvm-project/commit/8628ca29aa4714f99e865c99b9d510ad14897fdc
DIFF: https://github.com/llvm/llvm-project/commit/8628ca29aa4714f99e865c99b9d510ad14897fdc.diff

LOG: [libc] Fix UB in memory utils (#74295)

The [standard](https://eel.is/c++draft/expr.add#4.3) forbids forming
pointers to invalid objects even if the pointer is never read from or
written to. This patch makes sure that we don't do pointer arithmetic on
invalid pointers.


Co-authored-by: Vitaly Buka <vitalybuka at google.com>

Added: 
    

Modified: 
    libc/src/string/memory_utils/utils.h

Removed: 
    


################################################################################
diff  --git a/libc/src/string/memory_utils/utils.h b/libc/src/string/memory_utils/utils.h
index f70880ee853d3..9c293185a2e9f 100644
--- a/libc/src/string/memory_utils/utils.h
+++ b/libc/src/string/memory_utils/utils.h
@@ -341,9 +341,9 @@ void align_p1_to_next_boundary(T1 *__restrict &p1, T2 *__restrict &p2,
 }
 
 // Same as align_p1_to_next_boundary above but with a single pointer instead.
-template <size_t SIZE, typename T1>
-LIBC_INLINE void align_to_next_boundary(T1 *&p1, size_t &count) {
-  CPtr dummy;
+template <size_t SIZE, typename T>
+LIBC_INLINE void align_to_next_boundary(T *&p1, size_t &count) {
+  const T *dummy = p1;
   align_p1_to_next_boundary<SIZE>(p1, dummy, count);
 }
 


        


More information about the libc-commits mailing list