[parsing] Fix past the end position for streaming streams.

Also, as this is hard to track down, always DCHECK position after ReadBlock().

Change-Id: Ie32c3a311dd8df91f651b6d82ccacc7c95e6fde0
Reviewed-on: https://chromium-review.googlesource.com/528196
Commit-Queue: Marja Hölttä <marja@chromium.org>
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Daniel Vogelheim <vogelheim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45811}
This commit is contained in:
Wiktor Garbacz 2017-06-08 17:48:19 +02:00 committed by Commit Bot
parent 58ca2115ad
commit f4f723e818
4 changed files with 24 additions and 9 deletions

View File

@ -125,6 +125,7 @@ Victor Costan <costan@gmail.com>
Vlad Burlik <vladbph@gmail.com>
Vladimir Krivosheev <develar@gmail.com>
Vladimir Shutoff <vovan@shutoff.ru>
Wiktor Garbacz <wiktor.garbacz@gmail.com>
Yu Yin <xwafish@gmail.com>
Zac Hansen <xaxxon@gmail.com>
Zhongping Wang <kewpie.w.zp@gmail.com>

View File

@ -598,6 +598,7 @@ bool TwoByteExternalStreamingStream::ReadBlock() {
// Out of data? Return 0.
if (chunks_[chunk_no].byte_length == 0) {
buffer_pos_ = position;
buffer_cursor_ = buffer_start_;
buffer_end_ = buffer_start_;
return false;
@ -700,6 +701,7 @@ bool TwoByteExternalBufferedStream::ReadBlock() {
// Out of data? Return 0.
if (chunks_[chunk_no].byte_length == 0) {
buffer_pos_ = position;
buffer_cursor_ = buffer_start_;
buffer_end_ = buffer_start_;
return false;

View File

@ -43,7 +43,7 @@ class Utf16CharacterStream {
inline uc32 Advance() {
if (V8_LIKELY(buffer_cursor_ < buffer_end_)) {
return static_cast<uc32>(*(buffer_cursor_++));
} else if (ReadBlock()) {
} else if (ReadBlockChecked()) {
return static_cast<uc32>(*(buffer_cursor_++));
} else {
// Note: currently the following increment is necessary to avoid a
@ -102,6 +102,21 @@ class Utf16CharacterStream {
buffer_pos_(buffer_pos) {}
Utf16CharacterStream() : Utf16CharacterStream(nullptr, nullptr, nullptr, 0) {}
bool ReadBlockChecked() {
size_t position = pos();
USE(position);
bool success = ReadBlock();
// Post-conditions: 1, We should always be at the right position.
// 2, Cursor should be inside the buffer.
// 3, We should have more characters available iff success.
DCHECK_EQ(pos(), position);
DCHECK_LE(buffer_cursor_, buffer_end_);
DCHECK_LE(buffer_start_, buffer_cursor_);
DCHECK_EQ(success, buffer_cursor_ < buffer_end_);
return success;
}
void ReadBlockAt(size_t new_pos) {
// The callers of this method (Back/Back2/Seek) should handle the easy
// case (seeking within the current buffer), and we should only get here
@ -113,14 +128,8 @@ class Utf16CharacterStream {
// Change pos() to point to new_pos.
buffer_pos_ = new_pos;
buffer_cursor_ = buffer_start_;
bool success = ReadBlock();
USE(success);
// Post-conditions: 1, on success, we should be at the right position.
// 2, success == we should have more characters available.
DCHECK_IMPLIES(success, pos() == new_pos);
DCHECK_EQ(success, buffer_cursor_ < buffer_end_);
DCHECK_EQ(success, buffer_start_ < buffer_end_);
DCHECK_EQ(pos(), new_pos);
ReadBlockChecked();
}
// Read more data, and update buffer_*_ to point to it.

View File

@ -254,6 +254,9 @@ void TestCharacterStream(const char* reference, i::Utf16CharacterStream* stream,
CHECK_EQU(reference[i], stream->Advance());
}
CHECK_EQU(end, stream->pos());
CHECK_EQU(i::Utf16CharacterStream::kEndOfInput, stream->Advance());
CHECK_EQU(end + 1, stream->pos());
stream->Back();
// Pushback, re-read, pushback again.
while (i > end / 4) {