Do not allow allow reading beyond calldata

This commit is contained in:
Greg Hysen
2019-07-29 19:01:21 +02:00
parent db74db622e
commit 547322ae63
2 changed files with 19 additions and 2 deletions

View File

@@ -31,8 +31,13 @@ export class RawCalldata {
}
public popBytes(lengthInBytes: number): Buffer {
const value = this._value.slice(this._offset, this._offset + lengthInBytes);
this.setOffset(this._offset + lengthInBytes);
const popBegin = this._offset;
const popEnd = popBegin + lengthInBytes;
if (popEnd > this._value.byteLength) {
throw new Error(`Tried to decode beyond the end of calldata`);
}
const value = this._value.slice(popBegin, popEnd);
this.setOffset(popEnd);
return value;
}

View File

@@ -800,6 +800,18 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => {
const decodedArgs = dataType.decode(nullEncodedArgs);
expect(decodedArgs).to.be.deep.equal(args);
});
it('Should fail to decode if not enough bytes in calldata', async () => {
// Create DataType object
const testDataItem = { name: 'Integer (256)', type: 'int' };
const dataType = new AbiEncoder.Int(testDataItem);
const args = new BigNumber(0);
const encodedArgs = dataType.encode(args, encodingRules);
const encodedArgsTruncated = encodedArgs.substr(0, 60);
// Encode Args and validate result
expect(() => {
dataType.decode(encodedArgsTruncated);
}).to.throw();
});
});
describe('Unsigned Integer', () => {