Make all reinterpret_span<>() be truncating.
Chromium's base is never going to get a reinterpret_span implementation,
so the stricter behaviour advocated for during Chromium discussions is
not required here.
Change-Id: I4dbcb065980282d3378c87448c1bcde43948bb44
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/121492
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@google.com>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 86e1954..5b22355 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -551,8 +551,8 @@
return (charcode >= 32 && charcode < 127) ? 500 : 0;
}
uint16_t cid = CIDFromCharCode(charcode);
- auto lhv_span = fxcrt::truncating_reinterpret_span<const LowHighVal>(
- pdfium::make_span(m_WidthList));
+ auto lhv_span =
+ fxcrt::reinterpret_span<const LowHighVal>(pdfium::make_span(m_WidthList));
for (const auto& lhv : lhv_span) {
if (IsMetricForCID(lhv, cid)) {
return lhv.val;
@@ -562,7 +562,7 @@
}
int16_t CPDF_CIDFont::GetVertWidth(uint16_t cid) const {
- auto lhvxy_span = fxcrt::truncating_reinterpret_span<const LowHighValXY>(
+ auto lhvxy_span = fxcrt::reinterpret_span<const LowHighValXY>(
pdfium::make_span(m_VertMetrics));
for (const auto& lhvxy : lhvxy_span) {
if (IsMetricForCID(lhvxy, cid)) {
@@ -573,7 +573,7 @@
}
CFX_Point16 CPDF_CIDFont::GetVertOrigin(uint16_t cid) const {
- auto lhvxy_span = fxcrt::truncating_reinterpret_span<const LowHighValXY>(
+ auto lhvxy_span = fxcrt::reinterpret_span<const LowHighValXY>(
pdfium::make_span(m_VertMetrics));
for (const auto& lhvxy : lhvxy_span) {
if (IsMetricForCID(lhvxy, cid)) {
@@ -581,8 +581,8 @@
}
}
int width = m_DefaultWidth;
- auto lhv_span = fxcrt::truncating_reinterpret_span<const LowHighVal>(
- pdfium::make_span(m_WidthList));
+ auto lhv_span =
+ fxcrt::reinterpret_span<const LowHighVal>(pdfium::make_span(m_WidthList));
for (const auto& lhv : lhv_span) {
if (IsMetricForCID(lhv, cid)) {
width = lhv.val;
diff --git a/core/fpdfapi/page/cpdf_devicecs.cpp b/core/fpdfapi/page/cpdf_devicecs.cpp
index 5887dd3..15d6210 100644
--- a/core/fpdfapi/page/cpdf_devicecs.cpp
+++ b/core/fpdfapi/page/cpdf_devicecs.cpp
@@ -52,8 +52,7 @@
}
case Family::kDeviceRGB: {
const auto& rgb =
- fxcrt::truncating_reinterpret_span<const FX_RGB_STRUCT<float>>(pBuf)
- .front();
+ fxcrt::reinterpret_span<const FX_RGB_STRUCT<float>>(pBuf).front();
return FX_RGB_STRUCT<float>{
NormalizeChannel(rgb.red),
NormalizeChannel(rgb.green),
@@ -62,8 +61,7 @@
}
case Family::kDeviceCMYK: {
const auto& cmyk =
- fxcrt::truncating_reinterpret_span<const FX_CMYK_STRUCT<float>>(pBuf)
- .front();
+ fxcrt::reinterpret_span<const FX_CMYK_STRUCT<float>>(pBuf).front();
if (IsStdConversionEnabled()) {
return FX_RGB_STRUCT<float>{
1.0f - std::min(1.0f, cmyk.cyan + cmyk.key),
@@ -86,8 +84,7 @@
int image_width,
int image_height,
bool bTransMask) const {
- auto rgb_out =
- fxcrt::truncating_reinterpret_span<FX_RGB_STRUCT<uint8_t>>(dest_span);
+ auto rgb_out = fxcrt::reinterpret_span<FX_RGB_STRUCT<uint8_t>>(dest_span);
switch (GetFamily()) {
case Family::kDeviceGray:
CHECK(!bTransMask); // bTransMask only allowed for CMYK colorspaces.
@@ -106,8 +103,7 @@
break;
case Family::kDeviceCMYK: {
auto cmyk_in =
- fxcrt::truncating_reinterpret_span<const FX_CMYK_STRUCT<uint8_t>>(
- src_span);
+ fxcrt::reinterpret_span<const FX_CMYK_STRUCT<uint8_t>>(src_span);
if (bTransMask) {
// Compiler can't conclude src/dest don't overlap, avoid interleaved
// loads and stores by not using an auto& reference here.
diff --git a/core/fxcodec/bmp/cfx_bmpdecompressor.cpp b/core/fxcodec/bmp/cfx_bmpdecompressor.cpp
index a18c7b3..deb01b3 100644
--- a/core/fxcodec/bmp/cfx_bmpdecompressor.cpp
+++ b/core/fxcodec/bmp/cfx_bmpdecompressor.cpp
@@ -291,8 +291,7 @@
palette_.resize(palette_entries);
if (pal_type_ == PalType::kOld) {
auto src_pal_data =
- fxcrt::truncating_reinterpret_span<FX_BGR_STRUCT<uint8_t>, uint8_t>(
- src_pal);
+ fxcrt::reinterpret_span<FX_BGR_STRUCT<uint8_t>, uint8_t>(src_pal);
for (auto& dest : palette_) {
const auto& entry = src_pal_data.front();
dest = ArgbEncode(0x00, entry.red, entry.green, entry.blue);
@@ -300,8 +299,7 @@
}
} else {
auto src_pal_data =
- fxcrt::truncating_reinterpret_span<FX_BGRA_STRUCT<uint8_t>, uint8_t>(
- src_pal);
+ fxcrt::reinterpret_span<FX_BGRA_STRUCT<uint8_t>, uint8_t>(src_pal);
for (auto& dest : palette_) {
const auto& entry = src_pal_data.front();
dest = ArgbEncode(entry.alpha, entry.red, entry.green, entry.blue);
diff --git a/core/fxcodec/fx_codec.cpp b/core/fxcodec/fx_codec.cpp
index 9b5c1dd..dc0cbd7 100644
--- a/core/fxcodec/fx_codec.cpp
+++ b/core/fxcodec/fx_codec.cpp
@@ -25,12 +25,11 @@
int pixels) {
const size_t count = pdfium::checked_cast<size_t>(pixels);
auto dst_span =
- fxcrt::truncating_reinterpret_span<FX_RGB_STRUCT<uint8_t>>(pDestBuf)
- .first(count);
+ fxcrt::reinterpret_span<FX_RGB_STRUCT<uint8_t>>(pDestBuf).first(count);
const auto src_span =
- fxcrt::truncating_reinterpret_span<const FX_RGB_STRUCT<uint8_t>>(pSrcBuf)
- .first(count);
+ fxcrt::reinterpret_span<const FX_RGB_STRUCT<uint8_t>>(pSrcBuf).first(
+ count);
if (dst_span.data() == src_span.data()) {
for (auto& pix : dst_span) {
diff --git a/core/fxcrt/span_util.h b/core/fxcrt/span_util.h
index 5f5d592..448f41d 100644
--- a/core/fxcrt/span_util.h
+++ b/core/fxcrt/span_util.h
@@ -152,22 +152,13 @@
typename U,
typename = typename std::enable_if_t<std::is_const_v<T> ||
!std::is_const_v<U>>>
-inline pdfium::span<T> truncating_reinterpret_span(pdfium::span<U> s) noexcept {
+inline pdfium::span<T> reinterpret_span(pdfium::span<U> s) noexcept {
CHECK_EQ(reinterpret_cast<uintptr_t>(s.data()) % alignof(T), 0u);
// SAFETY: relies on correct conversion of size_bytes() result.
return UNSAFE_BUFFERS(pdfium::make_span(reinterpret_cast<T*>(s.data()),
s.size_bytes() / sizeof(T)));
}
-template <typename T,
- typename U,
- typename = typename std::enable_if_t<std::is_const_v<T> ||
- !std::is_const_v<U>>>
-inline pdfium::span<T> reinterpret_span(pdfium::span<U> s) noexcept {
- CHECK_EQ(s.size_bytes() % sizeof(T), 0u);
- return truncating_reinterpret_span<T, U>(s);
-}
-
} // namespace fxcrt
#endif // CORE_FXCRT_SPAN_UTIL_H_
diff --git a/core/fxcrt/span_util_unittest.cpp b/core/fxcrt/span_util_unittest.cpp
index 7ce7b38..ef9b9b9 100644
--- a/core/fxcrt/span_util_unittest.cpp
+++ b/core/fxcrt/span_util_unittest.cpp
@@ -188,11 +188,6 @@
EXPECT_EQ(converted[1], 0x62626262u);
}
-TEST(ReinterpretSpan, BadLength) {
- uint8_t ab[2] = {0x61, 0x62};
- EXPECT_DEATH(fxcrt::reinterpret_span<uint32_t>(pdfium::make_span(ab)), "");
-}
-
TEST(ReinterpretSpan, BadAlignment) {
uint8_t abcabc[6] = {0x61, 0x62, 0x63, 0x61, 0x62, 0x63};
EXPECT_DEATH(fxcrt::reinterpret_span<uint32_t>(
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h
index c9af2a3..5a3b7c1 100644
--- a/core/fxge/dib/cfx_dibbase.h
+++ b/core/fxge/dib/cfx_dibbase.h
@@ -53,7 +53,7 @@
template <typename T>
pdfium::span<const T> GetScanlineAs(int line) const {
- return fxcrt::truncating_reinterpret_span<const T>(GetScanline(line));
+ return fxcrt::reinterpret_span<const T>(GetScanline(line));
}
int GetWidth() const { return m_Width; }
diff --git a/core/fxge/dib/cfx_dibitmap.h b/core/fxge/dib/cfx_dibitmap.h
index 60f6391..cc4d987 100644
--- a/core/fxge/dib/cfx_dibitmap.h
+++ b/core/fxge/dib/cfx_dibitmap.h
@@ -63,7 +63,7 @@
template <typename T>
pdfium::span<T> GetWritableScanlineAs(int line) {
- return fxcrt::truncating_reinterpret_span<T>(GetWritableScanline(line));
+ return fxcrt::reinterpret_span<T>(GetWritableScanline(line));
}
void TakeOver(RetainPtr<CFX_DIBitmap>&& pSrcBitmap);