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

Alexander Richardson via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 21 07:40:19 PDT 2022


arichardson created this revision.
arichardson added a reviewer: libc++.
Herald added subscribers: jrtc27, kristof.beyls.
Herald added a project: All.
arichardson requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added 1 blocking reviewer(s): libc++.

Round-tripping pointers via size_t is not portable, the C/C++ standards
only require this to be valid when using (u)intptr_t. While touching
these lines also make use of the Clang 10+ __builtin_align_up to avoid
the need for reinterpret_cast.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134363

Files:
  libcxx/src/memory.cpp


Index: libcxx/src/memory.cpp
===================================================================
--- libcxx/src/memory.cpp
+++ libcxx/src/memory.cpp
@@ -194,7 +194,11 @@
     if (size <= space)
     {
         char* p1 = static_cast<char*>(ptr);
-        char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
+#if __has_builtin(__builtin_align_up)
+        char* p2 = __builtin_align_up(p1, alignment);
+#else
+        char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (alignment - 1)) & -alignment);
+#endif
         size_t d = static_cast<size_t>(p2 - p1);
         if (d <= space - size)
         {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134363.461894.patch
Type: text/x-patch
Size: 663 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220921/376a6f61/attachment.bin>


More information about the libcxx-commits mailing list