Consolidate UNSAFE_BUFFERS usage in CFX_Face
Add GetCharMaps() to make iterating through the charmaps easier. Use it
where appropriate to reduce the number of UNSAFE_BUFFERS() macros.
Change-Id: I99659203af98de595751aa406699a21200f87cbf
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/139133
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/cfx_face.cpp b/core/fxge/cfx_face.cpp
index ec0c244..c969112 100644
--- a/core/fxge/cfx_face.cpp
+++ b/core/fxge/cfx_face.cpp
@@ -930,21 +930,15 @@
}
int CFX_Face::GetCharMapPlatformIdByIndex(size_t index) const {
- CHECK_LT(index, GetCharMapCount());
- // SAFETY: required from library as enforced by check above.
- return UNSAFE_BUFFERS(GetRec()->charmaps[index]->platform_id);
+ return GetCharMaps()[index]->platform_id;
}
int CFX_Face::GetCharMapEncodingIdByIndex(size_t index) const {
- CHECK_LT(index, GetCharMapCount());
- // SAFETY: required from library as enforced by check above.
- return UNSAFE_BUFFERS(GetRec()->charmaps[index]->encoding_id);
+ return GetCharMaps()[index]->encoding_id;
}
fxge::FontEncoding CFX_Face::GetCharMapEncodingByIndex(size_t index) const {
- CHECK_LT(index, GetCharMapCount());
- // SAFETY: required from library as enforced by check above.
- return ToFontEncoding(UNSAFE_BUFFERS(GetRec()->charmaps[index]->encoding));
+ return ToFontEncoding(GetCharMaps()[index]->encoding);
}
size_t CFX_Face::GetCharMapCount() const {
@@ -953,6 +947,15 @@
: 0;
}
+pdfium::span<const FT_CharMap> CFX_Face::GetCharMaps() const {
+ size_t count = GetCharMapCount();
+ if (count == 0) {
+ return {};
+ }
+ // SAFETY: required from library to provide correct count.
+ return UNSAFE_BUFFERS({GetRec()->charmaps, count});
+}
+
void CFX_Face::SetCharMap(CharMap map) {
FT_Set_Charmap(GetRec(), static_cast<FT_CharMap>(map));
}
@@ -997,7 +1000,7 @@
void CFX_Face::AdjustVariationParams(int glyph_index,
int dest_width,
int weight) {
- DCHECK(dest_width >= 0);
+ DCHECK_GE(dest_width, 0);
FXFT_FaceRec* rec = GetRec();
ScopedFXFTMMVar variation_desc(rec);
diff --git a/core/fxge/cfx_face.h b/core/fxge/cfx_face.h
index 92b0030..e5957f1 100644
--- a/core/fxge/cfx_face.h
+++ b/core/fxge/cfx_face.h
@@ -168,6 +168,8 @@
void AdjustVariationParams(int glyph_index, int dest_width, int weight);
+ pdfium::span<const FT_CharMap> GetCharMaps() const;
+
#if BUILDFLAG(IS_ANDROID) || defined(PDF_ENABLE_XFA)
std::optional<std::array<uint8_t, 2>> GetOs2Panose();