From 154e18f9a862975abecebe77b8f5fb418418d14c Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 12 Apr 2018 18:33:55 +0000 Subject: Return pdfium::span 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 Commit-Queue: Tom Sepez --- xfa/fxfa/parser/cxfa_localevalue.cpp | 44 +++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'xfa/fxfa/parser') diff --git a/xfa/fxfa/parser/cxfa_localevalue.cpp b/xfa/fxfa/parser/cxfa_localevalue.cpp index 8ef67e5183..b129960f7d 100644 --- a/xfa/fxfa/parser/cxfa_localevalue.cpp +++ b/xfa/fxfa/parser/cxfa_localevalue.cpp @@ -682,32 +682,34 @@ void CXFA_LocaleValue::GetNumericFormat(WideString& wsFormat, int32_t nDecLen) { ASSERT(wsFormat.IsEmpty()); ASSERT(nIntLen >= -1 && nDecLen >= -1); - int32_t nTotalLen = (nIntLen >= 0 ? nIntLen : 2) + 1 + (nDecLen >= 0 ? nDecLen : 2) + (nDecLen == 0 ? 0 : 1); - wchar_t* lpBuf = wsFormat.GetBuffer(nTotalLen); - int32_t nPos = 0; - lpBuf[nPos++] = L's'; - - if (nIntLen == -1) { - lpBuf[nPos++] = L'z'; - lpBuf[nPos++] = L'*'; - } else { - while (nIntLen) { + { + // Span's lifetime must end before ReleaseBuffer() below. + pdfium::span lpBuf = wsFormat.GetBuffer(nTotalLen); + int32_t nPos = 0; + lpBuf[nPos++] = L's'; + + if (nIntLen == -1) { lpBuf[nPos++] = L'z'; - nIntLen--; + lpBuf[nPos++] = L'*'; + } else { + while (nIntLen) { + lpBuf[nPos++] = L'z'; + nIntLen--; + } } - } - if (nDecLen != 0) { - lpBuf[nPos++] = L'.'; - } - if (nDecLen == -1) { - lpBuf[nPos++] = L'z'; - lpBuf[nPos++] = L'*'; - } else { - while (nDecLen) { + if (nDecLen != 0) { + lpBuf[nPos++] = L'.'; + } + if (nDecLen == -1) { lpBuf[nPos++] = L'z'; - nDecLen--; + lpBuf[nPos++] = L'*'; + } else { + while (nDecLen) { + lpBuf[nPos++] = L'z'; + nDecLen--; + } } } wsFormat.ReleaseBuffer(nTotalLen); -- cgit v1.2.3