| Index: base/strings/utf_offset_string_conversions.cc
|
| diff --git a/base/strings/utf_offset_string_conversions.cc b/base/strings/utf_offset_string_conversions.cc
|
| index 79c00f03834ea80e58216787eee50dbfa47508b9..b91ee03832663e800d701409c22508024600c99d 100644
|
| --- a/base/strings/utf_offset_string_conversions.cc
|
| +++ b/base/strings/utf_offset_string_conversions.cc
|
| @@ -24,19 +24,20 @@ OffsetAdjuster::Adjustment::Adjustment(size_t original_offset,
|
| }
|
|
|
| // static
|
| -void OffsetAdjuster::AdjustOffsets(
|
| - const Adjustments& adjustments,
|
| - std::vector<size_t>* offsets_for_adjustment) {
|
| - if (!offsets_for_adjustment || adjustments.empty())
|
| - return;
|
| +void OffsetAdjuster::AdjustOffsets(const Adjustments& adjustments,
|
| + std::vector<size_t>* offsets_for_adjustment,
|
| + size_t limit) {
|
| + DCHECK(offsets_for_adjustment);
|
| for (std::vector<size_t>::iterator i(offsets_for_adjustment->begin());
|
| i != offsets_for_adjustment->end(); ++i)
|
| - AdjustOffset(adjustments, &(*i));
|
| + AdjustOffset(adjustments, &(*i), limit);
|
| }
|
|
|
| // static
|
| void OffsetAdjuster::AdjustOffset(const Adjustments& adjustments,
|
| - size_t* offset) {
|
| + size_t* offset,
|
| + size_t limit) {
|
| + DCHECK(offset);
|
| if (*offset == string16::npos)
|
| return;
|
| int adjustment = 0;
|
| @@ -51,6 +52,9 @@ void OffsetAdjuster::AdjustOffset(const Adjustments& adjustments,
|
| adjustment += static_cast<int>(i->original_length - i->output_length);
|
| }
|
| *offset -= adjustment;
|
| +
|
| + if (*offset > limit)
|
| + *offset = string16::npos;
|
| }
|
|
|
| // static
|
| @@ -236,9 +240,10 @@ string16 UTF8ToUTF16WithAdjustments(
|
| string16 UTF8ToUTF16AndAdjustOffsets(
|
| const base::StringPiece& utf8,
|
| std::vector<size_t>* offsets_for_adjustment) {
|
| - std::for_each(offsets_for_adjustment->begin(),
|
| - offsets_for_adjustment->end(),
|
| - LimitOffset<base::StringPiece>(utf8.length()));
|
| + for (size_t& offset : *offsets_for_adjustment) {
|
| + if (offset > utf8.length())
|
| + offset = string16::npos;
|
| + }
|
| OffsetAdjuster::Adjustments adjustments;
|
| string16 result = UTF8ToUTF16WithAdjustments(utf8, &adjustments);
|
| OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment);
|
| @@ -248,9 +253,10 @@ string16 UTF8ToUTF16AndAdjustOffsets(
|
| std::string UTF16ToUTF8AndAdjustOffsets(
|
| const base::StringPiece16& utf16,
|
| std::vector<size_t>* offsets_for_adjustment) {
|
| - std::for_each(offsets_for_adjustment->begin(),
|
| - offsets_for_adjustment->end(),
|
| - LimitOffset<base::StringPiece16>(utf16.length()));
|
| + for (size_t& offset : *offsets_for_adjustment) {
|
| + if (offset > utf16.length())
|
| + offset = string16::npos;
|
| + }
|
| std::string result;
|
| PrepareForUTF8Output(utf16.data(), utf16.length(), &result);
|
| OffsetAdjuster::Adjustments adjustments;
|
|
|