Prevent odd-length values from being passed in as bytesXX (#281).

This commit is contained in:
Richard Moore 2018-09-20 11:55:27 -04:00
parent f02f4bc0c0
commit 9d04f2c1eb
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
2 changed files with 17 additions and 0 deletions

View File

@ -271,4 +271,17 @@ describe('Test Invalid Input', function() {
}, 'long bytes32 throws an error');
});
// See: https://github.com/ethers-io/ethers.js/issues/281
it('fails to encode byteXX with odd input', function() {
assert.throws(function() {
var coder = ethers.utils.AbiCoder.defaultCoder;
var result = coder.encode([ 'bytes32' ], [ '0x1' ]);
console.log(result);
}, function(error) {
assert.equal(error.message, 'hex string cannot be odd-length', 'got odd bytes');
return true;
});
});
});

View File

@ -17,6 +17,7 @@ var utils = (function() {
getAddress: require('../utils/address').getAddress,
concat: convert.concat,
isHexString: convert.isHexString,
toUtf8Bytes: utf8.toUtf8Bytes,
toUtf8String: utf8.toUtf8String,
@ -411,6 +412,9 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
name: name,
type: name,
encode: function(value) {
if (utils.isHexString(value) && (value.length % 2) !== 0) {
throw new Error('hex string cannot be odd-length');
}
try {
value = utils.arrayify(value);