<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/76877>76877</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [libc] -Warray-bounds= in libc/src/string/memory_utils/op_builtin.h
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nickdesaulniers
      </td>
    </tr>
</table>

<pre>
    building with GCC produces the following diagnostic:
```
In static member function ‘static void __llvm_libc_18_0_0_git::builtin::Memcpy<Size>::block_offset(__llvm_libc_18_0_0_git::Ptr, __llvm_libc_18_0_0_git::CPtr, size_t) [with long unsigned int Size = 32]’,
    inlined from ‘static void __llvm_libc_18_0_0_git::builtin::Memcpy<Size>::tail(__llvm_libc_18_0_0_git::Ptr, __llvm_libc_18_0_0_git::CPtr, size_t) [with long unsigned int Size = 32]’ at /android0/llvm-project/libc/src/string/memory_utils/op_builtin.h:44:17,
    inlined from ‘static void __llvm_libc_18_0_0_git::builtin::Memcpy<Size>::loop_and_tail_offset(__llvm_libc_18_0_0_git::Ptr, __llvm_libc_18_0_0_git::CPtr, size_t, size_t) [with long unsigned int Size = 32]’ at /android0/llvm-project/libc/src/string/memory_utils/op_builtin.h:61:9,
    inlined from ‘static void __llvm_libc_18_0_0_git::builtin::Memcpy<Size>::loop_and_tail(__llvm_libc_18_0_0_git::Ptr, __llvm_libc_18_0_0_git::CPtr, size_t) [with long unsigned int Size = 32]’ at /android0/llvm-project/libc/src/string/memory_utils/op_builtin.h:66:32,
    inlined from ‘void __llvm_libc_18_0_0_git::inline_memcpy_x86_sse2_ge64(Ptr, CPtr, size_t)’ at /android0/llvm-project/libc/src/string/memory_utils/x86_64/inline_memcpy.h:57:44,
    inlined from ‘void __llvm_libc_18_0_0_git::inline_memcpy_x86(Ptr, CPtr, size_t)’ at /android0/llvm-project/libc/src/string/memory_utils/x86_64/inline_memcpy.h:193:41,
    inlined from ‘void __llvm_libc_18_0_0_git::inline_memcpy_x86_maybe_interpose_repmovsb(Ptr, CPtr, size_t)’ at /android0/llvm-project/libc/src/string/memory_utils/x86_64/inline_memcpy.h:204:29,
    inlined from ‘void __llvm_libc_18_0_0_git::inline_memcpy(void*, const void*, size_t)’ at /android0/llvm-project/libc/src/string/memory_utils/inline_memcpy.h:45:38,
    inlined from ‘int __llvm_libc_18_0_0_git::printf_core::Writer::write(__llvm_libc_18_0_0_git::cpp::string_view)’ at /android0/llvm-project/libc/src/stdio/printf_core/writer.h:98:20,
    inlined from ‘int __llvm_libc_18_0_0_git::printf_core::convert_inf_nan(Writer*, const FormatSection&)’ at /android0/llvm-project/libc/src/stdio/printf_core/float_inf_nan_converter.h:68:5:
/android0/llvm-project/libc/src/string/memory_utils/op_builtin.h:34:39: warning: array subscript [66, 9223372036854775807] is outside array bounds of ‘const char [4]’ [-Warray-bounds=]
   34 |       dst[i + offset] = src[i + offset];
      |                         ~~~~~~~~~~~~~~^
```

FWICT, `Memcpy::block_offset` falls back to a element by element copy if `LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE` is not defined (hence this only being reproducible with GCC).

IIUC, it looks like `inline_memcpy_x86` is generating a 32 element Memcpy (Size = 32), which indeed would read outside of src for a string literal like is done in `convert_inf_nan`

```c++
RET_IF_RESULT_NEGATIVE(writer->write(a == 'a' ? "inf" : "INF"));
```
in that case, I don't see why
```c++
RET_IF_RESULT_NEGATIVE(writer->write(sign_char));
```
doesn't warn since `sign_char` is a `char`.

---

I'm not sure yet if this can/should be fixed in `convert_inf_nan`, `Memcpy`, or somewhere else.  But we need to fix this to re-enable -Werror in #74506

cc @gchatelet @michaelrj-google 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWFtv4zYW_jX0y4EDmdTND36wHatrIBMUnUwH-0RQ1JHFhiINkorrPuxvX1Dy5NLsTqfYySBYwzBESTzn-75zIWnhvTo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