[libcxx-commits] [libcxx] 0a893cf - [libc++] Avoid relying on non-portable behaviour in std::align

Alex Richardson via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 3 05:46:24 PDT 2022


Author: Alex Richardson
Date: 2022-10-03T12:45:21Z
New Revision: 0a893cfb44e7d18296d6a7aff1ad11e615a89e93

URL: https://github.com/llvm/llvm-project/commit/0a893cfb44e7d18296d6a7aff1ad11e615a89e93
DIFF: https://github.com/llvm/llvm-project/commit/0a893cfb44e7d18296d6a7aff1ad11e615a89e93.diff

LOG: [libc++] Avoid relying on non-portable behaviour in std::align

Round-tripping pointers via size_t is not portable, the C/C++ standards
only require this to be valid when using (u)intptr_t.
Originally committed to the CHERI fork of LLVM as
https://github.com/CTSRD-CHERI/llvm-project/commit/dd01245185ab9e71b70b418bee8f11ea0199e1a3,
but I forgot to upstream the change. I rediscovered this issue due to a
compiler warning when building libc++ on a Arm Morello system.

Reviewed By: #libc, ldionne, philnik

Differential Revision: https://reviews.llvm.org/D134363

Added: 
    

Modified: 
    libcxx/src/memory.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 2dcbb3582a85..3fd5439049ab 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -194,7 +194,7 @@ align(size_t alignment, size_t size, void*& ptr, size_t& space)
     if (size <= space)
     {
         char* p1 = static_cast<char*>(ptr);
-        char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
+        char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (alignment - 1)) & -alignment);
         size_t d = static_cast<size_t>(p2 - p1);
         if (d <= space - size)
         {


        


More information about the libcxx-commits mailing list