From 3d523e3cf89440e2ffc6571b1c687ad5e3f0318f Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 16 Apr 2018 17:28:17 +0000 Subject: Revert "Return pdfium::span from ByteString::GetBuffer()." This reverts commit 80a6cbe0a427e155de8555bc867af745d10f9777. Reason for revert: too many abrts in beta branch. TBR: dsinclair@chromium.org Bug: 832557, 832978, 832992, 833062, 833097 Change-Id: I7d511dbb224ddc644be96ea2f3770ad6f73debf5 Reviewed-on: https://pdfium-review.googlesource.com/30792 Commit-Queue: Tom Sepez Reviewed-by: Tom Sepez Reviewed-by: dsinclair --- core/fpdfapi/page/cpdf_image.cpp | 17 ++++---- core/fpdfapi/parser/fpdf_parser_decode.cpp | 45 ++++++++++----------- core/fpdfapi/parser/fpdf_parser_utility.cpp | 50 ++++++++++------------- core/fxcrt/bytestring.cpp | 36 +++++++---------- core/fxcrt/bytestring.h | 6 +-- core/fxcrt/bytestring_unittest.cpp | 21 +++++----- core/fxcrt/widestring.cpp | 21 +++++----- core/fxge/cfx_folderfontinfo.cpp | 14 +++---- core/fxge/win32/fx_win32_dib.cpp | 62 ++++++++++++++--------------- fpdfsdk/cpdfsdk_formfillenvironment.cpp | 2 +- fxjs/cfxjse_formcalc_context.cpp | 16 +++----- fxjs/cjs_publicmethods.cpp | 9 ++--- 12 files changed, 128 insertions(+), 171 deletions(-) diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index b10134611c..ec826dc5ed 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -200,16 +200,13 @@ void CPDF_Image::SetImage(const RetainPtr& pBitmap) { pCS->AddNew("DeviceRGB"); pCS->AddNew(1); ByteString ct; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span pBuf = ct.GetBuffer(6); - pBuf[0] = (char)reset_r; - pBuf[1] = (char)reset_g; - pBuf[2] = (char)reset_b; - pBuf[3] = (char)set_r; - pBuf[4] = (char)set_g; - pBuf[5] = (char)set_b; - } + char* pBuf = ct.GetBuffer(6); + pBuf[0] = (char)reset_r; + pBuf[1] = (char)reset_g; + pBuf[2] = (char)reset_b; + pBuf[3] = (char)set_r; + pBuf[4] = (char)set_g; + pBuf[5] = (char)set_b; ct.ReleaseBuffer(6); pCS->AddNew(ct, true); } diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp index 2fdc4daa49..90dca2edcb 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp @@ -469,22 +469,20 @@ ByteString PDF_EncodeText(const wchar_t* pString, int len) { if (len == -1) len = wcslen(pString); - int i; ByteString result; - { - pdfium::span dest_buf = result.GetBuffer(len); - for (i = 0; i < len; ++i) { - int code; - for (code = 0; code < 256; ++code) { - if (PDFDocEncoding[code] == pString[i]) - break; - } - - if (code == 256) + char* dest_buf1 = result.GetBuffer(len); + int i; + for (i = 0; i < len; ++i) { + int code; + for (code = 0; code < 256; ++code) { + if (PDFDocEncoding[code] == pString[i]) break; - - dest_buf[i] = code; } + + if (code == 256) + break; + + dest_buf1[i] = code; } result.ReleaseBuffer(i); if (i == len) @@ -495,18 +493,15 @@ ByteString PDF_EncodeText(const wchar_t* pString, int len) { return result; } - size_t dest_index = 0; - size_t encLen = len * 2 + 2; - { - pdfium::span cspan = result.GetBuffer(encLen); - auto dest_buf = pdfium::make_span(reinterpret_cast(cspan.data()), - cspan.size()); - dest_buf[dest_index++] = 0xfe; - dest_buf[dest_index++] = 0xff; - for (int j = 0; j < len; ++j) { - dest_buf[dest_index++] = pString[j] >> 8; - dest_buf[dest_index++] = static_cast(pString[j]); - } + int encLen = len * 2 + 2; + + uint8_t* dest_buf2 = reinterpret_cast(result.GetBuffer(encLen)); + dest_buf2[0] = 0xfe; + dest_buf2[1] = 0xff; + dest_buf2 += 2; + for (int j = 0; j < len; ++j) { + *dest_buf2++ = pString[j] >> 8; + *dest_buf2++ = static_cast(pString[j]); } result.ReleaseBuffer(encLen); return result; diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp index b6b94aa84e..c486a76706 100644 --- a/core/fpdfapi/parser/fpdf_parser_utility.cpp +++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp @@ -93,23 +93,20 @@ ByteString PDF_NameDecode(const ByteStringView& bstr) { if (!bstr.Contains('#')) return ByteString(bstr); - size_t src_size = bstr.GetLength(); - size_t out_index = 0; + int size = bstr.GetLength(); ByteString result; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span pDest = result.GetBuffer(src_size); - for (size_t i = 0; i < src_size; i++) { - if (bstr[i] == '#' && i < src_size - 2) { - pDest[out_index++] = FXSYS_HexCharToInt(bstr[i + 1]) * 16 + - FXSYS_HexCharToInt(bstr[i + 2]); - i += 2; - } else { - pDest[out_index++] = bstr[i]; - } + char* pDestStart = result.GetBuffer(size); + char* pDest = pDestStart; + for (int i = 0; i < size; i++) { + if (bstr[i] == '#' && i < size - 2) { + *pDest++ = FXSYS_HexCharToInt(bstr[i + 1]) * 16 + + FXSYS_HexCharToInt(bstr[i + 2]); + i += 2; + } else { + *pDest++ = bstr[i]; } } - result.ReleaseBuffer(out_index); + result.ReleaseBuffer(static_cast(pDest - pDestStart)); return result; } @@ -131,23 +128,20 @@ ByteString PDF_NameEncode(const ByteString& orig) { return orig; ByteString res; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span dest_buf = res.GetBuffer(dest_len); - dest_len = 0; - for (i = 0; i < src_len; i++) { - uint8_t ch = src_buf[i]; - if (ch >= 0x80 || PDFCharIsWhitespace(ch) || ch == '#' || - PDFCharIsDelimiter(ch)) { - dest_buf[dest_len++] = '#'; - FXSYS_IntToTwoHexChars(ch, &dest_buf[dest_len]); - dest_len += 2; - continue; - } + char* dest_buf = res.GetBuffer(dest_len); + dest_len = 0; + for (i = 0; i < src_len; i++) { + uint8_t ch = src_buf[i]; + if (ch >= 0x80 || PDFCharIsWhitespace(ch) || ch == '#' || + PDFCharIsDelimiter(ch)) { + dest_buf[dest_len++] = '#'; + FXSYS_IntToTwoHexChars(ch, dest_buf + dest_len); + dest_len += 2; + } else { dest_buf[dest_len++] = ch; } - dest_buf[dest_len] = 0; } + dest_buf[dest_len] = 0; res.ReleaseBuffer(res.GetStringLength()); return res; } diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp index 872de065ba..f5687c591e 100644 --- a/core/fxcrt/bytestring.cpp +++ b/core/fxcrt/bytestring.cpp @@ -18,7 +18,6 @@ #include "core/fxcrt/fx_safe_types.h" #include "core/fxcrt/string_pool_template.h" #include "third_party/base/numerics/safe_math.h" -#include "third_party/base/span.h" #include "third_party/base/stl_util.h" template class fxcrt::StringDataTemplate; @@ -82,12 +81,9 @@ ByteString GetByteString(uint16_t codepage, const WideStringView& wstr) { return ByteString(); ByteString bstr; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span dest_buf = bstr.GetBuffer(dest_len); - FXSYS_WideCharToMultiByte(codepage, 0, wstr.unterminated_c_str(), src_len, - dest_buf.data(), dest_len, nullptr, nullptr); - } + char* dest_buf = bstr.GetBuffer(dest_len); + FXSYS_WideCharToMultiByte(codepage, 0, wstr.unterminated_c_str(), src_len, + dest_buf, dest_len, nullptr, nullptr); bstr.ReleaseBuffer(dest_len); return bstr; } @@ -124,21 +120,19 @@ ByteString ByteString::FormatV(const char* pFormat, va_list argList) { va_end(argListCopy); if (nMaxLen <= 0) - return ByteString(); + return ""; ByteString ret; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span buf = ret.GetBuffer(nMaxLen); - + char* buf = ret.GetBuffer(nMaxLen); + if (buf) { // In the following two calls, there's always space in the buffer for // a terminating NUL that's not included in nMaxLen. - memset(buf.data(), 0, nMaxLen + 1); + memset(buf, 0, nMaxLen + 1); va_copy(argListCopy, argList); - vsnprintf(buf.data(), nMaxLen + 1, pFormat, argListCopy); + vsnprintf(buf, nMaxLen + 1, pFormat, argListCopy); va_end(argListCopy); + ret.ReleaseBuffer(ret.GetStringLength()); } - ret.ReleaseBuffer(ret.GetStringLength()); return ret; } @@ -425,29 +419,29 @@ void ByteString::Reserve(size_t len) { GetBuffer(len); } -pdfium::span ByteString::GetBuffer(size_t nMinBufLength) { +char* ByteString::GetBuffer(size_t nMinBufLength) { if (!m_pData) { if (nMinBufLength == 0) - return pdfium::span(); + return nullptr; m_pData.Reset(StringData::Create(nMinBufLength)); m_pData->m_nDataLength = 0; m_pData->m_String[0] = 0; - return pdfium::span(m_pData->m_String, m_pData->m_nAllocLength); + return m_pData->m_String; } if (m_pData->CanOperateInPlace(nMinBufLength)) - return pdfium::span(m_pData->m_String, m_pData->m_nAllocLength); + return m_pData->m_String; nMinBufLength = std::max(nMinBufLength, m_pData->m_nDataLength); if (nMinBufLength == 0) - return pdfium::span(); + return nullptr; RetainPtr pNewData(StringData::Create(nMinBufLength)); pNewData->CopyContents(*m_pData); pNewData->m_nDataLength = m_pData->m_nDataLength; m_pData.Swap(pNewData); - return pdfium::span(m_pData->m_String, m_pData->m_nAllocLength); + return m_pData->m_String; } size_t ByteString::Delete(size_t index, size_t count) { diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h index 247238573d..ccb539b2ef 100644 --- a/core/fxcrt/bytestring.h +++ b/core/fxcrt/bytestring.h @@ -17,7 +17,6 @@ #include "core/fxcrt/string_data_template.h" #include "core/fxcrt/string_view_template.h" #include "third_party/base/optional.h" -#include "third_party/base/span.h" namespace fxcrt { @@ -154,10 +153,7 @@ class ByteString { size_t Delete(size_t index, size_t count = 1); void Reserve(size_t len); - - // Note: any modification of the string (including ReleaseBuffer()) may - // invalidate the span, which must not outlive its buffer. - pdfium::span GetBuffer(size_t len); + char* GetBuffer(size_t len); void ReleaseBuffer(size_t len); ByteString Mid(size_t first, size_t count) const; diff --git a/core/fxcrt/bytestring_unittest.cpp b/core/fxcrt/bytestring_unittest.cpp index d030535edd..f7e1559af9 100644 --- a/core/fxcrt/bytestring_unittest.cpp +++ b/core/fxcrt/bytestring_unittest.cpp @@ -843,23 +843,22 @@ TEST(ByteString, Reserve) { } TEST(ByteString, GetBuffer) { - ByteString str1; { - pdfium::span buffer = str1.GetBuffer(12); + ByteString str; + char* buffer = str.GetBuffer(12); // NOLINTNEXTLINE(runtime/printf) - strcpy(buffer.data(), "clams"); + strcpy(buffer, "clams"); + str.ReleaseBuffer(str.GetStringLength()); + EXPECT_EQ("clams", str); } - str1.ReleaseBuffer(str1.GetStringLength()); - EXPECT_EQ("clams", str1); - - ByteString str2("cl"); { - pdfium::span buffer = str2.GetBuffer(12); + ByteString str("cl"); + char* buffer = str.GetBuffer(12); // NOLINTNEXTLINE(runtime/printf) - strcpy(&buffer[2], "ams"); + strcpy(buffer + 2, "ams"); + str.ReleaseBuffer(str.GetStringLength()); + EXPECT_EQ("clams", str); } - str2.ReleaseBuffer(str2.GetStringLength()); - EXPECT_EQ("clams", str2); } TEST(ByteString, ReleaseBuffer) { diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index 25f253ea11..a3525593ee 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -667,21 +667,18 @@ ByteString WideString::UTF8Encode() const { } ByteString WideString::UTF16LE_Encode() const { - if (!m_pData) + if (!m_pData) { return ByteString("\0\0", 2); - - ByteString result; + } int len = m_pData->m_nDataLength; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span buffer = result.GetBuffer(len * 2 + 2); - for (int i = 0; i < len; i++) { - buffer[i * 2] = m_pData->m_String[i] & 0xff; - buffer[i * 2 + 1] = m_pData->m_String[i] >> 8; - } - buffer[len * 2] = 0; - buffer[len * 2 + 1] = 0; + ByteString result; + char* buffer = result.GetBuffer(len * 2 + 2); + for (int i = 0; i < len; i++) { + buffer[i * 2] = m_pData->m_String[i] & 0xff; + buffer[i * 2 + 1] = m_pData->m_String[i] >> 8; } + buffer[len * 2] = 0; + buffer[len * 2 + 1] = 0; result.ReleaseBuffer(len * 2 + 2); return result; } diff --git a/core/fxge/cfx_folderfontinfo.cpp b/core/fxge/cfx_folderfontinfo.cpp index b39c57637f..532824d248 100644 --- a/core/fxge/cfx_folderfontinfo.cpp +++ b/core/fxge/cfx_folderfontinfo.cpp @@ -44,15 +44,11 @@ const struct { }; ByteString FPDF_ReadStringFromFile(FILE* pFile, uint32_t size) { - ByteString result; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span buffer = result.GetBuffer(size); - if (!fread(buffer.data(), size, 1, pFile)) - return ByteString(); - } - result.ReleaseBuffer(size); - return result; + ByteString buffer; + if (!fread(buffer.GetBuffer(size), size, 1, pFile)) + return ByteString(); + buffer.ReleaseBuffer(size); + return buffer; } ByteString FPDF_LoadTableFromTT(FILE* pFile, diff --git a/core/fxge/win32/fx_win32_dib.cpp b/core/fxge/win32/fx_win32_dib.cpp index b6bed7a987..452d033f64 100644 --- a/core/fxge/win32/fx_win32_dib.cpp +++ b/core/fxge/win32/fx_win32_dib.cpp @@ -13,45 +13,41 @@ ByteString CFX_WindowsDIB::GetBitmapInfo( const RetainPtr& pBitmap) { + ByteString result; int len = sizeof(BITMAPINFOHEADER); - if (pBitmap->GetBPP() == 1 || pBitmap->GetBPP() == 8) + if (pBitmap->GetBPP() == 1 || pBitmap->GetBPP() == 8) { len += sizeof(DWORD) * (int)(1 << pBitmap->GetBPP()); - - ByteString result; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span cspan = result.GetBuffer(len); - BITMAPINFOHEADER* pbmih = reinterpret_cast(cspan.data()); - memset(pbmih, 0, sizeof(BITMAPINFOHEADER)); - pbmih->biSize = sizeof(BITMAPINFOHEADER); - pbmih->biBitCount = pBitmap->GetBPP(); - pbmih->biCompression = BI_RGB; - pbmih->biHeight = -(int)pBitmap->GetHeight(); - pbmih->biPlanes = 1; - pbmih->biWidth = pBitmap->GetWidth(); - if (pBitmap->GetBPP() == 8) { - uint32_t* pPalette = (uint32_t*)(pbmih + 1); - if (pBitmap->GetPalette()) { - for (int i = 0; i < 256; i++) { - pPalette[i] = pBitmap->GetPalette()[i]; - } - } else { - for (int i = 0; i < 256; i++) { - pPalette[i] = i * 0x010101; - } + } + BITMAPINFOHEADER* pbmih = (BITMAPINFOHEADER*)result.GetBuffer(len); + memset(pbmih, 0, sizeof(BITMAPINFOHEADER)); + pbmih->biSize = sizeof(BITMAPINFOHEADER); + pbmih->biBitCount = pBitmap->GetBPP(); + pbmih->biCompression = BI_RGB; + pbmih->biHeight = -(int)pBitmap->GetHeight(); + pbmih->biPlanes = 1; + pbmih->biWidth = pBitmap->GetWidth(); + if (pBitmap->GetBPP() == 8) { + uint32_t* pPalette = (uint32_t*)(pbmih + 1); + if (pBitmap->GetPalette()) { + for (int i = 0; i < 256; i++) { + pPalette[i] = pBitmap->GetPalette()[i]; } - } - if (pBitmap->GetBPP() == 1) { - uint32_t* pPalette = (uint32_t*)(pbmih + 1); - if (pBitmap->GetPalette()) { - pPalette[0] = pBitmap->GetPalette()[0]; - pPalette[1] = pBitmap->GetPalette()[1]; - } else { - pPalette[0] = 0; - pPalette[1] = 0xffffff; + } else { + for (int i = 0; i < 256; i++) { + pPalette[i] = i * 0x010101; } } } + if (pBitmap->GetBPP() == 1) { + uint32_t* pPalette = (uint32_t*)(pbmih + 1); + if (pBitmap->GetPalette()) { + pPalette[0] = pBitmap->GetPalette()[0]; + pPalette[1] = pBitmap->GetPalette()[1]; + } else { + pPalette[0] = 0; + pPalette[1] = 0xffffff; + } + } result.ReleaseBuffer(len); return result; } diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp index 262a56e2d1..c1846449cd 100644 --- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp +++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp @@ -25,7 +25,7 @@ FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE) { // to the embedder. Should the embedder modify it by accident, it won't // corrupt other shares of the string beyond |bsUTF16LE|. return reinterpret_cast( - bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()).data()); + bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); } CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment( diff --git a/fxjs/cfxjse_formcalc_context.cpp b/fxjs/cfxjse_formcalc_context.cpp index a6d151b468..85d0ef8d57 100644 --- a/fxjs/cfxjse_formcalc_context.cpp +++ b/fxjs/cfxjse_formcalc_context.cpp @@ -509,16 +509,12 @@ ByteString GUIDString(bool bSeparator) { data[6] = (data[6] & 0x0F) | 0x40; ByteString bsStr; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span pBuf = bsStr.GetBuffer(40); - size_t out_index = 0; - for (size_t i = 0; i < 16; ++i, out_index += 2) { - if (bSeparator && (i == 4 || i == 6 || i == 8 || i == 10)) - pBuf[out_index++] = L'-'; - - FXSYS_IntToTwoHexChars(data[i], &pBuf[out_index]); - } + char* pBuf = bsStr.GetBuffer(40); + for (int32_t i = 0; i < 16; ++i, pBuf += 2) { + if (bSeparator && (i == 4 || i == 6 || i == 8 || i == 10)) + *pBuf++ = L'-'; + + FXSYS_IntToTwoHexChars(data[i], pBuf); } bsStr.ReleaseBuffer(bSeparator ? 36 : 32); return bsStr; diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp index 8dc69c4a00..375eb6fd88 100644 --- a/fxjs/cjs_publicmethods.cpp +++ b/fxjs/cjs_publicmethods.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -1095,12 +1094,10 @@ CJS_Return CJS_PublicMethods::AFPercent_Format( if (iDec2 < 0) { ByteString zeros; - { - pdfium::span zeros_ptr = zeros.GetBuffer(abs(iDec2)); - std::fill(std::begin(zeros_ptr), std::end(zeros_ptr), '0'); - } - zeros.ReleaseBuffer(abs(iDec2)); + char* zeros_ptr = zeros.GetBuffer(abs(iDec2)); + memset(zeros_ptr, '0', abs(iDec2)); strValue = zeros + strValue; + iDec2 = 0; } int iMax = strValue.GetLength(); -- cgit v1.2.3