Fixed out-of-safe-range hexlify values to throw an exception (#420).

This commit is contained in:
Richard Moore 2019-02-08 17:06:46 -05:00
parent 9785eed8dd
commit 41c2c8a729
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
2 changed files with 26 additions and 0 deletions

View File

@ -168,6 +168,15 @@ export function hexlify(value: Arrayish | Hexable | number): string {
errors.throwError('cannot hexlify negative value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
// @TODO: Roll this into the above error as a numeric fault (overflow); next version, not backward compatible
// We can about (value == MAX_INT) to as well, since that may indicate we underflowed already
if (value >= 9007199254740991) {
errors.throwError("out-of-range", errors.NUMERIC_FAULT, {
operartion: "hexlify",
fault: "out-of-safe-range"
});
}
var hex = '';
while (value) {
hex = HexCharacters[value & 0x0f] + hex;

View File

@ -348,3 +348,20 @@ describe('Test BigNumber', function() {
].forEach(testAbs);
});
});
describe("Hexlify", function() {
it("hexlify on string of unsafe number", function() {
assert(ethers.utils.hexlify(ethers.utils.bigNumberify("9985956830000000000")), "0x8a953ed43a892c00", "hexlify on large BigNumber");
});
[9007199254740991, 9985956830000000000].forEach((value) => {
it('hexlify fails on unsafe number - ' + value, function() {
assert.throws(function() {
var result = ethers.utils.hexlify(value);
console.log('Result', result);
}, function(error) {
return (error.code === "NUMERIC_FAULT" && error.fault === "out-of-safe-range");
}, "hexlify throws on out-of-range value - " + value);
});
});
});