diff options
author | Tom Sepez <tsepez@chromium.org> | 2018-04-16 21:09:57 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-04-16 21:09:57 +0000 |
commit | 0d32b8fda53e02c1036d39f7290d4f59f2b58ca4 (patch) | |
tree | 820ea3b95c5ef566ea6c6c11fd6f113f0f895e70 /core/fxcrt/widestring.cpp | |
parent | 574ee81e426a7390e5cdf28f2fe8ec03f6c2da98 (diff) | |
download | pdfium-0d32b8fda53e02c1036d39f7290d4f59f2b58ca4.tar.xz |
Revert "Return pdfium::span<wchar_t> from WideString::GetBuffer()."
This reverts commit 154e18f9a862975abecebe77b8f5fb418418d14c.
Reason for revert: Generate CL to merge to beta branch
Original change's description:
> Return pdfium::span<wchar_t> from WideString::GetBuffer().
>
> Adds bounds checking "for free", but beware of span outliving
> a ReleaseBuffer() call. Scoping as such avoids the possibility
> of using an invalid span (and it is flagged as a lifetime issue).
>
> Change-Id: Ica63f4b1429823d0254ec6951aeaeb08160cb93c
> Reviewed-on: https://pdfium-review.googlesource.com/30310
> Reviewed-by: dsinclair <dsinclair@chromium.org>
> Commit-Queue: Tom Sepez <tsepez@chromium.org>
TBR=tsepez@chromium.org,dsinclair@chromium.org
# Not skipping CQ checks because original CL landed > 1 day ago.
Change-Id: Ie1ec9434215584a024538ca8edeb59dea555af48
Reviewed-on: https://pdfium-review.googlesource.com/30830
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt/widestring.cpp')
-rw-r--r-- | core/fxcrt/widestring.cpp | 65 |
1 files changed, 28 insertions, 37 deletions
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index a3525593ee..7b5bf66fd3 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -252,27 +252,22 @@ Optional<size_t> GuessSizeForVSWPrintf(const wchar_t* pFormat, Optional<WideString> TryVSWPrintf(size_t size, const wchar_t* pFormat, va_list argList) { - if (!size) + WideString str; + wchar_t* buffer = str.GetBuffer(size); + + // In the following two calls, there's always space in the buffer for + // a terminating NUL that's not included in nMaxLen. + // For vswprintf(), MSAN won't untaint the buffer on a truncated write's + // -1 return code even though the buffer is written. Probably just as well + // not to trust the vendor's implementation to write anything anyways. + // See https://crbug.com/705912. + memset(buffer, 0, (size + 1) * sizeof(wchar_t)); + int ret = vswprintf(buffer, size + 1, pFormat, argList); + + bool bSufficientBuffer = ret >= 0 || buffer[size - 1] == 0; + if (!bSufficientBuffer) return {}; - WideString str; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span<wchar_t> buffer = str.GetBuffer(size); - - // In the following two calls, there's always space in the WideString - // for a terminating NUL that's not included in the span. - // For vswprintf(), MSAN won't untaint the buffer on a truncated write's - // -1 return code even though the buffer is written. Probably just as well - // not to trust the vendor's implementation to write anything anyways. - // See https://crbug.com/705912. - memset(buffer.data(), 0, (size + 1) * sizeof(wchar_t)); - int ret = vswprintf(buffer.data(), size + 1, pFormat, argList); - - bool bSufficientBuffer = ret >= 0 || buffer[size - 1] == 0; - if (!bSufficientBuffer) - return {}; - } str.ReleaseBuffer(str.GetStringLength()); return {str}; } @@ -304,12 +299,9 @@ WideString GetWideString(uint16_t codepage, const ByteStringView& bstr) { return WideString(); WideString wstr; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span<wchar_t> dest_buf = wstr.GetBuffer(dest_len); - FXSYS_MultiByteToWideChar(codepage, 0, bstr.unterminated_c_str(), src_len, - dest_buf.data(), dest_len); - } + wchar_t* dest_buf = wstr.GetBuffer(dest_len); + FXSYS_MultiByteToWideChar(codepage, 0, bstr.unterminated_c_str(), src_len, + dest_buf, dest_len); wstr.ReleaseBuffer(dest_len); return wstr; } @@ -594,29 +586,29 @@ void WideString::Reserve(size_t len) { GetBuffer(len); } -pdfium::span<wchar_t> WideString::GetBuffer(size_t nMinBufLength) { +wchar_t* WideString::GetBuffer(size_t nMinBufLength) { if (!m_pData) { if (nMinBufLength == 0) - return pdfium::span<wchar_t>(); + return nullptr; m_pData.Reset(StringData::Create(nMinBufLength)); m_pData->m_nDataLength = 0; m_pData->m_String[0] = 0; - return pdfium::span<wchar_t>(m_pData->m_String, m_pData->m_nAllocLength); + return m_pData->m_String; } if (m_pData->CanOperateInPlace(nMinBufLength)) - return pdfium::span<wchar_t>(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<wchar_t>(); + return nullptr; RetainPtr<StringData> pNewData(StringData::Create(nMinBufLength)); pNewData->CopyContents(*m_pData); pNewData->m_nDataLength = m_pData->m_nDataLength; m_pData.Swap(pNewData); - return pdfium::span<wchar_t>(m_pData->m_String, m_pData->m_nAllocLength); + return m_pData->m_String; } size_t WideString::Delete(size_t index, size_t count) { @@ -893,15 +885,14 @@ WideString WideString::FromUTF8(const ByteStringView& str) { // static WideString WideString::FromUTF16LE(const unsigned short* wstr, size_t wlen) { - if (!wstr || wlen == 0) + if (!wstr || wlen == 0) { return WideString(); + } WideString result; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span<wchar_t> buf = result.GetBuffer(wlen); - for (size_t i = 0; i < wlen; i++) - buf[i] = wstr[i]; + wchar_t* buf = result.GetBuffer(wlen); + for (size_t i = 0; i < wlen; i++) { + buf[i] = wstr[i]; } result.ReleaseBuffer(wlen); return result; |