Avoid more future unsafe buffer usage in StringDataTemplate. Use bounds-checked copies and indexing. Change-Id: I93952c294733bf5ae883ceeb0bb540b9f87a12c6 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/116890 Reviewed-by: Lei Zhang <thestig@chromium.org> Reviewed-by: Thomas Sepez <tsepez@google.com> Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/string_data_template.cpp b/core/fxcrt/string_data_template.cpp index 1f0046f..30ca0a6 100644 --- a/core/fxcrt/string_data_template.cpp +++ b/core/fxcrt/string_data_template.cpp
@@ -12,9 +12,9 @@ #include "core/fxcrt/check.h" #include "core/fxcrt/check_op.h" -#include "core/fxcrt/fx_memcpy_wrappers.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_safe_types.h" +#include "core/fxcrt/span_util.h" namespace fxcrt { @@ -63,32 +63,30 @@ template <typename CharType> void StringDataTemplate<CharType>::CopyContents( const StringDataTemplate& other) { - DCHECK(other.m_nDataLength <= m_nAllocLength); - memcpy(m_String, other.m_String, - (other.m_nDataLength + 1) * sizeof(CharType)); + fxcrt::spancpy(capacity_span(), + other.capacity_span().first(other.m_nDataLength + 1)); } template <typename CharType> void StringDataTemplate<CharType>::CopyContents( pdfium::span<const CharType> str) { - FXSYS_memcpy(m_String, str.data(), str.size_bytes()); - m_String[str.size()] = 0; + fxcrt::spancpy(capacity_span(), str); + capacity_span()[str.size()] = 0; } template <typename CharType> void StringDataTemplate<CharType>::CopyContentsAt( size_t offset, pdfium::span<const CharType> str) { - FXSYS_memcpy(m_String + offset, str.data(), str.size_bytes()); - m_String[offset + str.size()] = 0; + fxcrt::spancpy(capacity_span().subspan(offset), str); + capacity_span()[offset + str.size()] = 0; } template <typename CharType> StringDataTemplate<CharType>::StringDataTemplate(size_t dataLen, size_t allocLen) : m_nDataLength(dataLen), m_nAllocLength(allocLen) { - DCHECK_LE(dataLen, allocLen); - m_String[dataLen] = 0; + capacity_span()[dataLen] = 0; } // Instantiate.