Skip to content

[libc] -Warray-bounds= in libc/src/string/memory_utils/op_builtin.h #76877

Closed
@nickdesaulniers

Description

@nickdesaulniers

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

RET_IF_RESULT_NEGATIVE(writer->write(a == 'a' ? "inf" : "INF"));

in that case, I don't see why

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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions