Fixed long fixed-length bytes from overflowing encoded ABI. (#237)

This commit is contained in:
Richard Moore 2018-07-26 18:02:42 -04:00
parent a8283ea99f
commit 29f3d2dea8
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
3 changed files with 24 additions and 3 deletions

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

@ -260,4 +260,15 @@ describe('Test Invalid Input', function() {
return true; return true;
}, 'null bytes throws an error'); }, 'null bytes throws an error');
}); });
it('fails to encode fixed bytes that are out of range', function() {
assert.throws(function() {
var result = coder.encode([ 'bytes32' ], [ '0x012345678901234567890123456789012345678901234567890123456789012345' ]);
console.log('Result', result);
}, function(error) {
assert.equal(error.reason, 'invalid bytes32 value', 'got invalid bytes32');
return true;
}, 'long bytes32 throws an error');
});
}); });

@ -341,10 +341,11 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
}); });
} }
value = value.toTwos(size * 8).maskn(size * 8); value = value.toTwos(size * 8).maskn(size * 8);
//value = value.toTwos(size * 8).maskn(size * 8);
if (signed) { if (signed) {
value = value.fromTwos(size * 8).toTwos(256); value = value.fromTwos(size * 8).toTwos(256);
} }
return utils.padZeros(utils.arrayify(value), 32); return utils.padZeros(utils.arrayify(value), 32);
}, },
decode: function(data, offset) { decode: function(data, offset) {
@ -412,6 +413,14 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
encode: function(value) { encode: function(value) {
try { try {
value = utils.arrayify(value); value = utils.arrayify(value);
// @TODO: In next major change, the value.length MUST equal the
// length, but that is a backward-incompatible change, so here
// we just check for things that can cause problems.
if (value.length > 32) {
throw new Error('too many bytes for field');
}
} catch (error) { } catch (error) {
errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, { errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, {
arg: localName, arg: localName,
@ -419,7 +428,8 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
value: error.value value: error.value
}); });
} }
if (length === 32) { return value; }
if (value.length === 32) { return value; }
var result = new Uint8Array(32); var result = new Uint8Array(32);
result.set(value); result.set(value);