Skip to content

Commit 41c2c8a

Browse files
committed
Fixed out-of-safe-range hexlify values to throw an exception (ethers-io#420).
1 parent 9785eed commit 41c2c8a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src.ts/utils/bytes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ export function hexlify(value: Arrayish | Hexable | number): string {
168168
errors.throwError('cannot hexlify negative value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
169169
}
170170

171+
// @TODO: Roll this into the above error as a numeric fault (overflow); next version, not backward compatible
172+
// We can about (value == MAX_INT) to as well, since that may indicate we underflowed already
173+
if (value >= 9007199254740991) {
174+
errors.throwError("out-of-range", errors.NUMERIC_FAULT, {
175+
operartion: "hexlify",
176+
fault: "out-of-safe-range"
177+
});
178+
}
179+
171180
var hex = '';
172181
while (value) {
173182
hex = HexCharacters[value & 0x0f] + hex;

tests/test-utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,20 @@ describe('Test BigNumber', function() {
348348
].forEach(testAbs);
349349
});
350350
});
351+
352+
describe("Hexlify", function() {
353+
it("hexlify on string of unsafe number", function() {
354+
assert(ethers.utils.hexlify(ethers.utils.bigNumberify("9985956830000000000")), "0x8a953ed43a892c00", "hexlify on large BigNumber");
355+
});
356+
357+
[9007199254740991, 9985956830000000000].forEach((value) => {
358+
it('hexlify fails on unsafe number - ' + value, function() {
359+
assert.throws(function() {
360+
var result = ethers.utils.hexlify(value);
361+
console.log('Result', result);
362+
}, function(error) {
363+
return (error.code === "NUMERIC_FAULT" && error.fault === "out-of-safe-range");
364+
}, "hexlify throws on out-of-range value - " + value);
365+
});
366+
});
367+
});

0 commit comments

Comments
 (0)