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

@ -78,6 +78,9 @@ export class BufferWriter {
}
writeSlice(slice: Buffer): void {
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);
}
@ -131,8 +134,12 @@ export class BufferReader {
}
readSlice(n: number): Buffer {
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(): Buffer {