[libc-commits] [libc] [libc] fix -Warray-bounds in block_offset (PR #77001)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Jan 4 12:47:20 PST 2024


https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/77001

GCC reports an instance of -Warray-bounds in block_offset.  Reimplement
block_offset in terms of memcpy_inline which was created to avoid this
diagnostic. See the linked issue for the full trace of diagnostic.

Fixes: https://github.com/llvm/llvm-project/issues/76877


>From c8fada8251e1b6795940074764f3062248435866 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 4 Jan 2024 12:44:37 -0800
Subject: [PATCH] [libc] fix -Warray-bounds in block_offset

GCC reports an instance of -Warray-bounds in block_offset.  Reimplement
block_offset in terms of memcpy_inline which was created to avoid this
diagnostic. See the linked issue for the full trace of diagnostic.

Fixes: https://github.com/llvm/llvm-project/issues/76877
---
 libc/src/string/memory_utils/op_builtin.h | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/libc/src/string/memory_utils/op_builtin.h b/libc/src/string/memory_utils/op_builtin.h
index 16c9f519c37e3f..3c17eef781e576 100644
--- a/libc/src/string/memory_utils/op_builtin.h
+++ b/libc/src/string/memory_utils/op_builtin.h
@@ -26,13 +26,7 @@ template <size_t Size> struct Memcpy {
   static constexpr size_t SIZE = Size;
   LIBC_INLINE static void block_offset(Ptr __restrict dst, CPtr __restrict src,
                                        size_t offset) {
-#ifdef LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
-    return __builtin_memcpy_inline(dst + offset, src + offset, SIZE);
-#else
-    // The codegen may be suboptimal.
-    for (size_t i = 0; i < Size; ++i)
-      dst[i + offset] = src[i + offset];
-#endif
+    memcpy_inline<Size>(dst + offset, src + offset);
   }
 
   LIBC_INLINE static void block(Ptr __restrict dst, CPtr __restrict src) {



More information about the libc-commits mailing list