summaryrefslogtreecommitdiff
path: root/core/fxcrt/cfx_blockbuffer.cpp
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-04-12 18:33:55 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-04-12 18:33:55 +0000
commit154e18f9a862975abecebe77b8f5fb418418d14c (patch)
tree18e9381c1a4324abcd98296e1e1714c2f926e006 /core/fxcrt/cfx_blockbuffer.cpp
parent7f821c11081fe90346823333622253ec7949b583 (diff)
downloadpdfium-154e18f9a862975abecebe77b8f5fb418418d14c.tar.xz
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>
Diffstat (limited to 'core/fxcrt/cfx_blockbuffer.cpp')
-rw-r--r--core/fxcrt/cfx_blockbuffer.cpp55
1 files changed, 29 insertions, 26 deletions
diff --git a/core/fxcrt/cfx_blockbuffer.cpp b/core/fxcrt/cfx_blockbuffer.cpp
index 13134f0e7b..6a7d98aa18 100644
--- a/core/fxcrt/cfx_blockbuffer.cpp
+++ b/core/fxcrt/cfx_blockbuffer.cpp
@@ -77,36 +77,39 @@ WideString CFX_BlockBuffer::GetTextData(size_t start, size_t length) const {
size_t maybeDataLength = m_BufferSize - 1 - m_StartPosition;
if (start > maybeDataLength)
return WideString();
- length = std::min(length, maybeDataLength);
- WideString wsTextData;
- wchar_t* pBuf = wsTextData.GetBuffer(length);
- if (!pBuf)
+ length = std::min(length, maybeDataLength);
+ if (!length)
return WideString();
- size_t startBlock = 0;
- size_t startInner = 0;
- std::tie(startBlock, startInner) = TextDataIndex2BufIndex(start);
-
- size_t endBlock = 0;
- size_t endInner = 0;
- std::tie(endBlock, endInner) = TextDataIndex2BufIndex(start + length);
-
- size_t pointer = 0;
- for (size_t i = startBlock; i <= endBlock; ++i) {
- size_t bufferPointer = 0;
- size_t copyLength = kAllocStep;
- if (i == startBlock) {
- copyLength -= startInner;
- bufferPointer = startInner;
+ WideString wsTextData;
+ {
+ // Span's lifetime must end before ReleaseBuffer() below.
+ pdfium::span<wchar_t> pBuf = wsTextData.GetBuffer(length);
+ size_t startBlock = 0;
+ size_t startInner = 0;
+ std::tie(startBlock, startInner) = TextDataIndex2BufIndex(start);
+
+ size_t endBlock = 0;
+ size_t endInner = 0;
+ std::tie(endBlock, endInner) = TextDataIndex2BufIndex(start + length);
+
+ size_t pointer = 0;
+ for (size_t i = startBlock; i <= endBlock; ++i) {
+ size_t bufferPointer = 0;
+ size_t copyLength = kAllocStep;
+ if (i == startBlock) {
+ copyLength -= startInner;
+ bufferPointer = startInner;
+ }
+ if (i == endBlock)
+ copyLength -= ((kAllocStep - 1) - endInner);
+
+ wchar_t* pBlockBuf = m_BlockArray[i].get();
+ memcpy(&pBuf[pointer], pBlockBuf + bufferPointer,
+ copyLength * sizeof(wchar_t));
+ pointer += copyLength;
}
- if (i == endBlock)
- copyLength -= ((kAllocStep - 1) - endInner);
-
- wchar_t* pBlockBuf = m_BlockArray[i].get();
- memcpy(pBuf + pointer, pBlockBuf + bufferPointer,
- copyLength * sizeof(wchar_t));
- pointer += copyLength;
}
wsTextData.ReleaseBuffer(length);
return wsTextData;