summaryrefslogtreecommitdiff
path: root/core/fpdfapi/parser/fpdf_parser_utility.cpp
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-04-17 17:19:30 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-04-17 17:19:30 +0000
commit1dbfe996b946f0d2a1afc52669ff5fca22a85070 (patch)
treef4037b8c4c46725a9c73276f9c58cd78883b79dc /core/fpdfapi/parser/fpdf_parser_utility.cpp
parent2617056df6d6e1d0f17031f0c9db09f9192cb0fa (diff)
downloadpdfium-1dbfe996b946f0d2a1afc52669ff5fca22a85070.tar.xz
Re-land "Return pdfium::span<char> from ByteString::GetBuffer().""
This reverts commit 3d523e3cf89440e2ffc6571b1c687ad5e3f0318f. Fixes bounding errors now caught by tests. Change-Id: I4d0f1791bdcc45a10615a62abf7a4d20e7e538f2 Reviewed-on: https://pdfium-review.googlesource.com/30799 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fpdfapi/parser/fpdf_parser_utility.cpp')
-rw-r--r--core/fpdfapi/parser/fpdf_parser_utility.cpp51
1 files changed, 28 insertions, 23 deletions
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp
index c486a76706..2c0b0818f1 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp
@@ -93,20 +93,23 @@ ByteString PDF_NameDecode(const ByteStringView& bstr) {
if (!bstr.Contains('#'))
return ByteString(bstr);
- int size = bstr.GetLength();
+ size_t src_size = bstr.GetLength();
+ size_t out_index = 0;
ByteString result;
- 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];
+ {
+ // Span's lifetime must end before ReleaseBuffer() below.
+ pdfium::span<char> pDest = result.GetBuffer(src_size);
+ for (size_t i = 0; i < src_size; i++) {
+ if (bstr[i] == '#' && i + 2 < src_size) {
+ pDest[out_index++] = FXSYS_HexCharToInt(bstr[i + 1]) * 16 +
+ FXSYS_HexCharToInt(bstr[i + 2]);
+ i += 2;
+ } else {
+ pDest[out_index++] = bstr[i];
+ }
}
}
- result.ReleaseBuffer(static_cast<size_t>(pDest - pDestStart));
+ result.ReleaseBuffer(out_index);
return result;
}
@@ -128,21 +131,23 @@ ByteString PDF_NameEncode(const ByteString& orig) {
return orig;
ByteString res;
- 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 {
+ {
+ // Span's lifetime must end before ReleaseBuffer() below.
+ pdfium::span<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;
+ continue;
+ }
dest_buf[dest_len++] = ch;
}
}
- dest_buf[dest_len] = 0;
- res.ReleaseBuffer(res.GetStringLength());
+ res.ReleaseBuffer(dest_len);
return res;
}