Navigation Menu

Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
deps: fix out-of-band write in utf8 decoder
Browse files Browse the repository at this point in the history
Originally reported by: Kris Reeves <kris.re@bbhmedia.com>

Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
indutny authored and Julien Gilli committed Jul 4, 2015
1 parent c3e02ae commit 78b0e30
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions deps/v8/src/unicode-inl.h
Expand Up @@ -155,6 +155,7 @@ unsigned Utf8::Length(uchar c, int previous) {

Utf8DecoderBase::Utf8DecoderBase()
: unbuffered_start_(NULL),
unbuffered_length_(0),
utf16_length_(0),
last_byte_of_buffer_unused_(false) {}

Expand Down Expand Up @@ -194,8 +195,7 @@ unsigned Utf8Decoder<kBufferSize>::WriteUtf16(uint16_t* data,
if (length <= buffer_length) return length;
DCHECK(unbuffered_start_ != NULL);
// Copy the rest the slow way.
WriteUtf16Slow(unbuffered_start_,
data + buffer_length,
WriteUtf16Slow(unbuffered_start_, unbuffered_length_, data + buffer_length,
length - buffer_length);
return length;
}
Expand Down
9 changes: 8 additions & 1 deletion deps/v8/src/unicode.cc
Expand Up @@ -265,6 +265,7 @@ void Utf8DecoderBase::Reset(uint16_t* buffer,
// Assume everything will fit in the buffer and stream won't be needed.
last_byte_of_buffer_unused_ = false;
unbuffered_start_ = NULL;
unbuffered_length_ = 0;
bool writing_to_buffer = true;
// Loop until stream is read, writing to buffer as long as buffer has space.
unsigned utf16_length = 0;
Expand All @@ -291,6 +292,7 @@ void Utf8DecoderBase::Reset(uint16_t* buffer,
// Just wrote last character of buffer
writing_to_buffer = false;
unbuffered_start_ = stream;
unbuffered_length_ = stream_length;
}
continue;
}
Expand All @@ -300,20 +302,24 @@ void Utf8DecoderBase::Reset(uint16_t* buffer,
writing_to_buffer = false;
last_byte_of_buffer_unused_ = true;
unbuffered_start_ = stream - cursor;
unbuffered_length_ = stream_length + cursor;
}
utf16_length_ = utf16_length;
}


void Utf8DecoderBase::WriteUtf16Slow(const uint8_t* stream,
unsigned stream_length,
uint16_t* data,
unsigned data_length) {
while (data_length != 0) {
unsigned cursor = 0;
uint32_t character = Utf8::ValueOf(stream, Utf8::kMaxEncodedSize, &cursor);

uint32_t character = Utf8::ValueOf(stream, stream_length, &cursor);
// There's a total lack of bounds checking for stream
// as it was already done in Reset.
stream += cursor;
stream_length -= cursor;
if (character > unibrow::Utf16::kMaxNonSurrogateCharCode) {
*data++ = Utf16::LeadSurrogate(character);
*data++ = Utf16::TrailSurrogate(character);
Expand All @@ -324,6 +330,7 @@ void Utf8DecoderBase::WriteUtf16Slow(const uint8_t* stream,
data_length -= 1;
}
}
DCHECK(stream_length >= 0);
}


Expand Down
6 changes: 3 additions & 3 deletions deps/v8/src/unicode.h
Expand Up @@ -172,10 +172,10 @@ class Utf8DecoderBase {
unsigned buffer_length,
const uint8_t* stream,
unsigned stream_length);
static void WriteUtf16Slow(const uint8_t* stream,
uint16_t* data,
unsigned length);
static void WriteUtf16Slow(const uint8_t* stream, unsigned stream_length,
uint16_t* data, unsigned length);
const uint8_t* unbuffered_start_;
unsigned unbuffered_length_;
unsigned utf16_length_;
bool last_byte_of_buffer_unused_;
private:
Expand Down

1 comment on commit 78b0e30

@cjihrig
Copy link

@cjihrig cjihrig commented on 78b0e30 Jul 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, it does not affect 0.10.

Please sign in to comment.