summaryrefslogtreecommitdiff
path: root/core/fxcrt/cfx_widestring.cpp
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-08-02 14:44:17 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-08-02 19:02:41 +0000
commitdb14532fb2637b34f0926b6c3a931132854f53bb (patch)
tree322bdae3c8ee7812a182f88def802e28fbc34675 /core/fxcrt/cfx_widestring.cpp
parent4a0cbf9f0cb4f7e3bcbae68f82201b44fc94c231 (diff)
downloadpdfium-db14532fb2637b34f0926b6c3a931132854f53bb.tar.xz
Rewrite how string Insert() methods handle out of bound indices
The existing behaviour was to clamp the provided index to the valid bounds. This would lead to programming errors being hidden, since inserting would still do something even if the index calculation was wrong. The behaviour of these methods has been changed to instead early return when this occurs, returning the old length value. The caller can check if the call to Insert actually did anything by comparing the returned value to the length before calling insert. All of the existing calls to Insert have been tested by running all of the tests with asserts in the Insert method to check the index is in bounds. Additionally the call sites have been manually inspected. The majority of them are of the form Insert(0, foo) and the rest tend to be in a loop advancing from 0 to length. Convenience methods InsertAtFront/InsertAtBack have been added to handle calling Insert when the intent is for the character to be added to the beginning or end of the string. Existing call sites to Insert that do this have been converted. This work was originally being performed to check if there would be any issues in these methods with making FX_STRSIZE unsigned. BUG=pdfium:828 Change-Id: I60cee5ad45338aa8ed46569de7bcc78a76db18f7 Reviewed-on: https://pdfium-review.googlesource.com/9870 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt/cfx_widestring.cpp')
-rw-r--r--core/fxcrt/cfx_widestring.cpp24
1 files changed, 12 insertions, 12 deletions
diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp
index b83752369b..6c079b354e 100644
--- a/core/fxcrt/cfx_widestring.cpp
+++ b/core/fxcrt/cfx_widestring.cpp
@@ -680,18 +680,18 @@ void CFX_WideString::Format(const wchar_t* pFormat, ...) {
va_end(argList);
}
-FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, wchar_t ch) {
- FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0;
- nIndex = std::max(nIndex, 0);
- nIndex = std::min(nIndex, nNewLength);
- nNewLength++;
-
- ReallocBeforeWrite(nNewLength);
- wmemmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex,
- nNewLength - nIndex);
- m_pData->m_String[nIndex] = ch;
- m_pData->m_nDataLength = nNewLength;
- return nNewLength;
+FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE index, wchar_t ch) {
+ const FX_STRSIZE cur_length = m_pData ? m_pData->m_nDataLength : 0;
+ if (index != pdfium::clamp(index, 0, cur_length))
+ return cur_length;
+
+ const FX_STRSIZE new_length = cur_length + 1;
+ ReallocBeforeWrite(new_length);
+ wmemmove(m_pData->m_String + index + 1, m_pData->m_String + index,
+ new_length - index);
+ m_pData->m_String[index] = ch;
+ m_pData->m_nDataLength = new_length;
+ return new_length;
}
CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const {