Check write/read Slice out of bounds

This commit is contained in:
junderw 2020-01-15 14:14:02 +09:00
parent 717166e668
commit 5679a4b455
No known key found for this signature in database
GPG key ID: B256185D3A971908
3 changed files with 22 additions and 2 deletions

View file

@ -67,6 +67,9 @@ class BufferWriter {
this.offset += varuint.encode.bytes;
}
writeSlice(slice) {
if (this.buffer.length < this.offset + slice.length) {
throw new Error('Cannot write slice out of bounds');
}
this.offset += slice.copy(this.buffer, this.offset);
}
writeVarSlice(slice) {
@ -114,8 +117,12 @@ class BufferReader {
return vi;
}
readSlice(n) {
if (this.buffer.length < this.offset + n) {
throw new Error('Cannot read slice out of bounds');
}
const result = this.buffer.slice(this.offset, this.offset + n);
this.offset += n;
return this.buffer.slice(this.offset - n, this.offset);
return result;
}
readVarSlice() {
return this.readSlice(this.readVarInt());