Skip to content

Commit 78de5f8

Browse files
bnoordhuisrvagg
authored andcommitted
deps: fix out-of-band write in utf8 decoder
Originally reported by: Kris Reeves <[email protected]> This is a back-port of commit 030f804 from the master branch. Reviewed-By: Rod Vagg <[email protected]>
1 parent d8f260d commit 78de5f8

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

deps/v8/src/unicode-decoder.cc

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void Utf8DecoderBase::Reset(uint16_t* buffer, unsigned buffer_length,
1515
// Assume everything will fit in the buffer and stream won't be needed.
1616
last_byte_of_buffer_unused_ = false;
1717
unbuffered_start_ = NULL;
18+
unbuffered_length_ = 0;
1819
bool writing_to_buffer = true;
1920
// Loop until stream is read, writing to buffer as long as buffer has space.
2021
unsigned utf16_length = 0;
@@ -41,6 +42,7 @@ void Utf8DecoderBase::Reset(uint16_t* buffer, unsigned buffer_length,
4142
// Just wrote last character of buffer
4243
writing_to_buffer = false;
4344
unbuffered_start_ = stream;
45+
unbuffered_length_ = stream_length;
4446
}
4547
continue;
4648
}
@@ -50,19 +52,22 @@ void Utf8DecoderBase::Reset(uint16_t* buffer, unsigned buffer_length,
5052
writing_to_buffer = false;
5153
last_byte_of_buffer_unused_ = true;
5254
unbuffered_start_ = stream - cursor;
55+
unbuffered_length_ = stream_length + cursor;
5356
}
5457
utf16_length_ = utf16_length;
5558
}
5659

5760

58-
void Utf8DecoderBase::WriteUtf16Slow(const uint8_t* stream, uint16_t* data,
61+
void Utf8DecoderBase::WriteUtf16Slow(const uint8_t* stream,
62+
unsigned stream_length, uint16_t* data,
5963
unsigned data_length) {
6064
while (data_length != 0) {
6165
unsigned cursor = 0;
62-
uint32_t character = Utf8::ValueOf(stream, Utf8::kMaxEncodedSize, &cursor);
66+
uint32_t character = Utf8::ValueOf(stream, stream_length, &cursor);
6367
// There's a total lack of bounds checking for stream
6468
// as it was already done in Reset.
6569
stream += cursor;
70+
stream_length -= cursor;
6671
if (character > unibrow::Utf16::kMaxNonSurrogateCharCode) {
6772
*data++ = Utf16::LeadSurrogate(character);
6873
*data++ = Utf16::TrailSurrogate(character);
@@ -73,6 +78,7 @@ void Utf8DecoderBase::WriteUtf16Slow(const uint8_t* stream, uint16_t* data,
7378
data_length -= 1;
7479
}
7580
}
81+
DCHECK(stream_length >= 0);
7682
}
7783

7884
} // namespace unibrow

deps/v8/src/unicode-decoder.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ class Utf8DecoderBase {
2323
// The first buffer_length utf16 chars are cached in the buffer.
2424
void Reset(uint16_t* buffer, unsigned buffer_length, const uint8_t* stream,
2525
unsigned stream_length);
26-
static void WriteUtf16Slow(const uint8_t* stream, uint16_t* data,
27-
unsigned length);
26+
static void WriteUtf16Slow(const uint8_t* stream, unsigned stream_length,
27+
uint16_t* data, unsigned length);
2828
const uint8_t* unbuffered_start_;
29+
unsigned unbuffered_length_;
2930
unsigned utf16_length_;
3031
bool last_byte_of_buffer_unused_;
3132

@@ -48,6 +49,7 @@ class Utf8Decoder : public Utf8DecoderBase {
4849

4950
Utf8DecoderBase::Utf8DecoderBase()
5051
: unbuffered_start_(NULL),
52+
unbuffered_length_(0),
5153
utf16_length_(0),
5254
last_byte_of_buffer_unused_(false) {}
5355

@@ -85,7 +87,7 @@ unsigned Utf8Decoder<kBufferSize>::WriteUtf16(uint16_t* data,
8587
if (length <= buffer_length) return length;
8688
DCHECK(unbuffered_start_ != NULL);
8789
// Copy the rest the slow way.
88-
WriteUtf16Slow(unbuffered_start_, data + buffer_length,
90+
WriteUtf16Slow(unbuffered_start_, unbuffered_length_, data + buffer_length,
8991
length - buffer_length);
9092
return length;
9193
}

0 commit comments

Comments
 (0)