Remove #pragma unsafe_buffer_usage from CPDF_DocPageData.
Bug: pdfium:2154
Change-Id: I1703fa81ba21bb5c0d307648aaa874f2e568d9df
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/118952
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp
index f7c74cb..1f57f07 100644
--- a/core/fpdfapi/page/cpdf_docpagedata.cpp
+++ b/core/fpdfapi/page/cpdf_docpagedata.cpp
@@ -4,14 +4,10 @@
// 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/page/cpdf_docpagedata.h"
#include <algorithm>
+#include <array>
#include <memory>
#include <set>
#include <utility>
@@ -43,6 +39,7 @@
#include "core/fxcrt/fx_memory.h"
#include "core/fxcrt/fx_safe_types.h"
#include "core/fxcrt/scoped_set_insertion.h"
+#include "core/fxcrt/span.h"
#include "core/fxge/cfx_font.h"
#include "core/fxge/cfx_fontmapper.h"
#include "core/fxge/cfx_substfont.h"
@@ -538,7 +535,8 @@
} else {
size_t i = CalculateEncodingDict(charset, pBaseDict.Get());
if (i < std::size(kFX_CharsetUnicodes)) {
- const uint16_t* pUnicodes = kFX_CharsetUnicodes[i].m_pUnicodes;
+ pdfium::span<const uint16_t> pUnicodes =
+ kFX_CharsetUnicodes[i].m_pUnicodes;
for (int j = 0; j < 128; j++) {
int glyph_index = pEncoding->GlyphFromCharCode(pUnicodes[j]);
int char_width = pFont->GetGlyphWidth(glyph_index);
@@ -566,12 +564,13 @@
if (pFont->GetSubstFont()) {
nStemV = pFont->GetSubstFont()->m_Weight / 5;
} else {
- static const char stem_chars[] = {'i', 'I', '!', '1'};
- const size_t count = std::size(stem_chars);
- uint32_t glyph = pEncoding->GlyphFromCharCode(stem_chars[0]);
+ static constexpr char kStemChars[] = {'i', 'I', '!', '1'};
+ static constexpr pdfium::span<const char> kStemSpan{kStemChars};
+ uint32_t glyph = pEncoding->GlyphFromCharCode(kStemSpan.front());
+ const auto remaining = kStemSpan.subspan(1);
nStemV = pFont->GetGlyphWidth(glyph);
- for (size_t i = 1; i < count; i++) {
- glyph = pEncoding->GlyphFromCharCode(stem_chars[i]);
+ for (auto ch : remaining) {
+ glyph = pEncoding->GlyphFromCharCode(ch);
int width = pFont->GetGlyphWidth(glyph);
if (width > 0 && width < nStemV)
nStemV = width;
@@ -624,8 +623,8 @@
int ascend = ptm->otmrcFontBox.top;
int descend = ptm->otmrcFontBox.bottom;
int capheight = ptm->otmsCapEmHeight;
- int bbox[4] = {ptm->otmrcFontBox.left, ptm->otmrcFontBox.bottom,
- ptm->otmrcFontBox.right, ptm->otmrcFontBox.top};
+ std::array<int, 4> bbox = {{ptm->otmrcFontBox.left, ptm->otmrcFontBox.bottom,
+ ptm->otmrcFontBox.right, ptm->otmrcFontBox.top}};
FX_Free(tm_buf);
basefont.Replace(" ", "");
auto pBaseDict = GetDocument()->NewIndirect<CPDF_Dictionary>();
@@ -639,11 +638,12 @@
} else {
CalculateEncodingDict(eCharset, pBaseDict.Get());
}
- int char_widths[224];
- GetCharWidth(hDC, 32, 255, char_widths);
+ std::array<int, 224> char_widths;
+ GetCharWidth(hDC, 32, 255, char_widths.data());
auto pWidths = pdfium::MakeRetain<CPDF_Array>();
- for (size_t i = 0; i < 224; i++)
- pWidths->AppendNew<CPDF_Number>(char_widths[i]);
+ for (const auto char_width : char_widths) {
+ pWidths->AppendNew<CPDF_Number>(char_width);
+ }
ProcessNonbCJK(pBaseDict, pLogFont->lfWeight > FW_MEDIUM,
pLogFont->lfItalic != 0, basefont, std::move(pWidths));
} else {
@@ -654,8 +654,9 @@
});
}
auto pBBox = pdfium::MakeRetain<CPDF_Array>();
- for (int i = 0; i < 4; i++)
- pBBox->AppendNew<CPDF_Number>(bbox[i]);
+ for (const auto bound : bbox) {
+ pBBox->AppendNew<CPDF_Number>(bound);
+ }
RetainPtr<CPDF_Dictionary> pFontDesc =
CalculateFontDesc(GetDocument(), basefont, flags, italicangle, ascend,
descend, std::move(pBBox), pLogFont->lfWeight / 5);
@@ -686,7 +687,7 @@
auto pArray = pEncodingDict->SetNewFor<CPDF_Array>("Differences");
pArray->AppendNew<CPDF_Number>(128);
- const uint16_t* pUnicodes = kFX_CharsetUnicodes[i].m_pUnicodes;
+ pdfium::span<const uint16_t> pUnicodes = kFX_CharsetUnicodes[i].m_pUnicodes;
for (int j = 0; j < 128; j++) {
ByteString name = AdobeNameFromUnicode(pUnicodes[j]);
pArray->AppendNew<CPDF_Name>(name.IsEmpty() ? ".notdef" : name);
diff --git a/core/fxcrt/fx_codepage.cpp b/core/fxcrt/fx_codepage.cpp
index b8b8d03..82af097 100644
--- a/core/fxcrt/fx_codepage.cpp
+++ b/core/fxcrt/fx_codepage.cpp
@@ -204,7 +204,7 @@
} // namespace
-const FX_CharsetUnicodes kFX_CharsetUnicodes[8] = {
+const std::array<FX_CharsetUnicodes, 8> kFX_CharsetUnicodes = {{
{FX_Charset::kThai, kFX_MSDOSThaiUnicodes},
{FX_Charset::kMSWin_EasternEuropean, kFX_MSWinEasternEuropeanUnicodes},
{FX_Charset::kMSWin_Cyrillic, kFX_MSWinCyrillicUnicodes},
@@ -213,7 +213,7 @@
{FX_Charset::kMSWin_Hebrew, kFX_MSWinHebrewUnicodes},
{FX_Charset::kMSWin_Arabic, kFX_MSWinArabicUnicodes},
{FX_Charset::kMSWin_Baltic, kFX_MSWinBalticUnicodes},
-};
+}};
FX_CodePage FX_GetACP() {
#if BUILDFLAG(IS_WIN)
diff --git a/core/fxcrt/fx_codepage.h b/core/fxcrt/fx_codepage.h
index 8368d37..349cc6d 100644
--- a/core/fxcrt/fx_codepage.h
+++ b/core/fxcrt/fx_codepage.h
@@ -9,11 +9,12 @@
#include <stdint.h>
+#include <array>
+
// Prove consistency with incomplete forward definitions.
#include "core/fxcrt/fx_codepage_forward.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/span.h"
-#include "core/fxcrt/unowned_ptr_exclusion.h"
enum class FX_CodePage : uint16_t {
kDefANSI = 0,
@@ -104,10 +105,10 @@
// Hi-bytes to unicode codepoint mapping for various code pages.
struct FX_CharsetUnicodes {
FX_Charset m_Charset;
- UNOWNED_PTR_EXCLUSION const uint16_t* m_pUnicodes; // POD struct.
+ pdfium::span<const uint16_t> m_pUnicodes;
};
-extern const FX_CharsetUnicodes kFX_CharsetUnicodes[8];
+extern const std::array<FX_CharsetUnicodes, 8> kFX_CharsetUnicodes;
FX_CodePage FX_GetACP();
FX_CodePage FX_GetCodePageFromCharset(FX_Charset charset);