Browse Source
Check write/read Slice out of bounds
psbt-tx-getters
junderw
5 years ago
No known key found for this signature in database
GPG Key ID: B256185D3A971908
3 changed files with
22 additions and
2 deletions
-
src/bufferutils.js
-
test/bufferutils.spec.ts
-
ts_src/bufferutils.ts
|
|
@ -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()); |
|
|
|
|
|
@ -209,6 +209,9 @@ describe('bufferutils', () => { |
|
|
|
testBuffer(bufferWriter, expectedBuffer, expectedOffset); |
|
|
|
}); |
|
|
|
testBuffer(bufferWriter, expectedBuffer); |
|
|
|
assert.throws(() => { |
|
|
|
bufferWriter.writeSlice(Buffer.from([0, 0])); |
|
|
|
}, /^Error: Cannot write slice out of bounds$/); |
|
|
|
}); |
|
|
|
|
|
|
|
it('writeVarSlice', () => { |
|
|
@ -421,6 +424,9 @@ describe('bufferutils', () => { |
|
|
|
const val = bufferReader.readSlice(v.length); |
|
|
|
testValue(bufferReader, val, Buffer.from(v), expectedOffset); |
|
|
|
}); |
|
|
|
assert.throws(() => { |
|
|
|
bufferReader.readSlice(2); |
|
|
|
}, /^Error: Cannot read slice out of bounds$/); |
|
|
|
}); |
|
|
|
|
|
|
|
it('readVarSlice', () => { |
|
|
|
|
|
@ -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 { |
|
|
|