Convert to std::array<> in CPDF_SimpleFont. Use more modern types to get libc++ hardening "for free". Change-Id: I30b0822738f787055cc1742a74eb707a88c380e1 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/118812 Reviewed-by: Thomas Sepez <tsepez@google.com> Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp index 29a89f8..dee1a0a 100644 --- a/core/fpdfapi/font/cpdf_simplefont.cpp +++ b/core/fpdfapi/font/cpdf_simplefont.cpp
@@ -4,11 +4,6 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#if defined(UNSAFE_BUFFERS_BUILD) -// TODO(crbug.com/pdfium/2153): resolve buffer safety issues. -#pragma allow_unsafe_buffers -#endif - #include "core/fpdfapi/font/cpdf_simplefont.h" #include <algorithm> @@ -41,10 +36,9 @@ CPDF_SimpleFont::CPDF_SimpleFont(CPDF_Document* pDocument, RetainPtr<CPDF_Dictionary> pFontDict) : CPDF_Font(pDocument, std::move(pFontDict)) { - memset(m_CharWidth, 0xff, sizeof(m_CharWidth)); - memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex)); - for (size_t i = 0; i < std::size(m_CharBBox); ++i) - m_CharBBox[i] = FX_RECT(-1, -1, -1, -1); + m_CharWidth.fill(0xffff); + m_GlyphIndex.fill(0xffff); + m_CharBBox.fill(FX_RECT(-1, -1, -1, -1)); } CPDF_SimpleFont::~CPDF_SimpleFont() = default; @@ -248,14 +242,13 @@ } if (FontStyleIsAllCaps(m_Flags)) { - static const unsigned char kLowercases[][2] = { - {'a', 'z'}, {0xe0, 0xf6}, {0xf8, 0xfd}}; - for (size_t range = 0; range < std::size(kLowercases); ++range) { - const auto& lower = kLowercases[range]; - for (int i = lower[0]; i <= lower[1]; ++i) { - if (m_GlyphIndex[i] != 0xffff && m_pFontFile) + static const std::array<std::pair<uint8_t, uint8_t>, 3> kLowercases = { + {{'a', 'z'}, {0xe0, 0xf6}, {0xf8, 0xfd}}}; + for (const auto& lower : kLowercases) { + for (int i = lower.first; i <= lower.second; ++i) { + if (m_GlyphIndex[i] != 0xffff && m_pFontFile) { continue; - + } int j = i - 32; m_GlyphIndex[i] = m_GlyphIndex[j]; if (m_CharWidth[j]) {
diff --git a/core/fpdfapi/font/cpdf_simplefont.h b/core/fpdfapi/font/cpdf_simplefont.h index e0d0400..23bd6ff 100644 --- a/core/fpdfapi/font/cpdf_simplefont.h +++ b/core/fpdfapi/font/cpdf_simplefont.h
@@ -9,6 +9,7 @@ #include <stdint.h> +#include <array> #include <vector> #include "core/fpdfapi/font/cpdf_font.h" @@ -50,9 +51,9 @@ FontEncoding m_BaseEncoding = FontEncoding::kBuiltin; bool m_bUseFontWidth = false; std::vector<ByteString> m_CharNames; - uint16_t m_GlyphIndex[kInternalTableSize]; - uint16_t m_CharWidth[kInternalTableSize]; - FX_RECT m_CharBBox[kInternalTableSize]; + std::array<uint16_t, kInternalTableSize> m_GlyphIndex; + std::array<uint16_t, kInternalTableSize> m_CharWidth; + std::array<FX_RECT, kInternalTableSize> m_CharBBox; }; #endif // CORE_FPDFAPI_FONT_CPDF_SIMPLEFONT_H_
diff --git a/core/fpdfapi/font/cpdf_truetypefont.cpp b/core/fpdfapi/font/cpdf_truetypefont.cpp index 5123121..5745b49 100644 --- a/core/fpdfapi/font/cpdf_truetypefont.cpp +++ b/core/fpdfapi/font/cpdf_truetypefont.cpp
@@ -245,7 +245,7 @@ if (start_char < 0 || start_char > 255) return; - auto* it = std::begin(m_GlyphIndex); + auto it = std::begin(m_GlyphIndex); std::fill(it, it + start_char, 0); uint16_t glyph = 3; for (int charcode = start_char; charcode < 256; charcode++, glyph++)
diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp index 50efda7..e87b1ed 100644 --- a/core/fpdfapi/font/cpdf_type1font.cpp +++ b/core/fpdfapi/font/cpdf_type1font.cpp
@@ -76,7 +76,7 @@ RetainPtr<CPDF_Dictionary> pFontDict) : CPDF_SimpleFont(pDocument, std::move(pFontDict)) { #if BUILDFLAG(IS_APPLE) - memset(m_ExtGID, 0xff, sizeof(m_ExtGID)); + m_ExtGID.fill(0xffff); #endif } @@ -168,8 +168,9 @@ } if (bGotOne) { #if BUILDFLAG(IS_APPLE) - if (!bCoreText) - memcpy(m_ExtGID, m_GlyphIndex, sizeof(m_ExtGID)); + if (!bCoreText) { + m_ExtGID = m_GlyphIndex; + } #endif return; } @@ -200,8 +201,7 @@ } #if BUILDFLAG(IS_APPLE) if (!bCoreText) { - fxcrt::spancpy(pdfium::make_span(m_ExtGID), - pdfium::make_span(m_GlyphIndex)); + m_ExtGID = m_GlyphIndex; } #endif return; @@ -281,8 +281,9 @@ } } #if BUILDFLAG(IS_APPLE) - if (!bCoreText) - memcpy(m_ExtGID, m_GlyphIndex, sizeof(m_ExtGID)); + if (!bCoreText) { + m_ExtGID = m_GlyphIndex; + } #endif return; } @@ -309,8 +310,9 @@ } } #if BUILDFLAG(IS_APPLE) - if (!bCoreText) - memcpy(m_ExtGID, m_GlyphIndex, sizeof(m_ExtGID)); + if (!bCoreText) { + m_ExtGID = m_GlyphIndex; + } #endif }
diff --git a/core/fpdfapi/font/cpdf_type1font.h b/core/fpdfapi/font/cpdf_type1font.h index 60e1ffa..c9ad6a2 100644 --- a/core/fpdfapi/font/cpdf_type1font.h +++ b/core/fpdfapi/font/cpdf_type1font.h
@@ -9,6 +9,8 @@ #include <stdint.h> +#include <array> + #include "build/build_config.h" #include "core/fpdfapi/font/cpdf_simplefont.h" #include "core/fxcrt/retain_ptr.h" @@ -46,7 +48,7 @@ void SetExtGID(const char* name, uint32_t charcode); void CalcExtGID(uint32_t charcode); - uint16_t m_ExtGID[kInternalTableSize]; + std::array<uint16_t, kInternalTableSize> m_ExtGID; #endif std::optional<CFX_FontMapper::StandardFont> m_Base14Font;