2
0
mirror of synced 2025-02-25 04:25:16 +00:00

Fixed bool and number types not throwing an exception in the ABI decoding for empty bytes.

This commit is contained in:
Richard Moore 2018-03-17 17:39:45 -04:00
parent 46fd2deb8b
commit 1ec8f9cf85
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
3 changed files with 23 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "ethers", "name": "ethers",
"version": "3.0.6", "version": "3.0.7",
"description": "Ethereum wallet library.", "description": "Ethereum wallet library.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -228,3 +228,16 @@ describe('Test Interface Signatures', function() {
}) })
}); });
}); });
describe('Test Invalid Input', function() {
var coder = ethers.utils.AbiCoder.defaultCoder;
it('null input failed', function() {
assert.throws(function() {
var result = coder.decode([ 'bool' ], '0x');
console.log(result);
}, function(error) {
assert.equal(error.message, 'invalid bool', 'got invalid bool');
return true;
}, 'null bytes throws an error');
});
});

View File

@ -70,6 +70,7 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
return utils.padZeros(utils.arrayify(value), 32); return utils.padZeros(utils.arrayify(value), 32);
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid ' + name); }
var junkLength = 32 - size; var junkLength = 32 - size;
var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32)); var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32));
if (signed) { if (signed) {
@ -98,7 +99,14 @@ var coderBoolean = function(coerceFunc, localName) {
return uint256Coder.encode(value ? 1: 0); return uint256Coder.encode(value ? 1: 0);
}, },
decode: function(data, offset) { decode: function(data, offset) {
try {
var result = uint256Coder.decode(data, offset); var result = uint256Coder.decode(data, offset);
} catch (error) {
if (error.message === 'invalid uint256') {
throwError('invalid bool');
}
throw error;
}
return { return {
consumed: result.consumed, consumed: result.consumed,
value: coerceFunc('boolean', !result.value.isZero()) value: coerceFunc('boolean', !result.value.isZero())