Check write/read Slice out of bounds
This commit is contained in:
parent
717166e668
commit
5679a4b455
3 changed files with 22 additions and 2 deletions
|
@ -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());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue