@0x/contracts-staking: Fix overflow w/ LibFixedMath._mul(-1, -2*255).

This commit is contained in:
Lawrence Forman 2019-11-01 12:51:19 -04:00
parent 8e6d92cad5
commit 4f56d68689
3 changed files with 19 additions and 4 deletions

View File

@ -21,6 +21,10 @@
{
"note": "The fallback function in `StakingProxy` reverts if there is no staking contract attached",
"pr": 2310
},
{
"note": "Fix overflow w/ `LibFixedMath._mul(-1, -2*255)",
"pr": 2311
}
]
},

View File

@ -349,7 +349,7 @@ library LibFixedMath {
return 0;
}
c = a * b;
if (c / a != b) {
if (c / a != b || c / b != a) {
LibRichErrors.rrevert(LibFixedMathRichErrors.BinOpError(
LibFixedMathRichErrors.BinOpErrorCodes.MULTIPLICATION_OVERFLOW,
a,

View File

@ -184,14 +184,14 @@ blockchainTests('LibFixedMath unit tests', env => {
return expect(tx).to.revertWith(expectedError);
});
it('mulDiv(MIN_FIXED, int(-1), int(1)) throws', async () => {
const [a, n, d] = [MIN_FIXED_VALUE, -1, 1];
it('mulDiv(int(-1), MIN_FIXED, int(1)) throws', async () => {
const [a, n, d] = [-1, MIN_FIXED_VALUE, 1];
const expectedError = new FixedMathRevertErrors.BinOpError(
FixedMathRevertErrors.BinOpErrorCodes.MultiplicationOverflow,
a,
n,
);
const tx = testContract.mulDiv.callAsync(a, new BigNumber(n), new BigNumber(d));
const tx = testContract.mulDiv.callAsync(new BigNumber(a), n, new BigNumber(d));
return expect(tx).to.revertWith(expectedError);
});
@ -506,6 +506,17 @@ blockchainTests('LibFixedMath unit tests', env => {
return expect(tx).to.revertWith(expectedError);
});
it('int(-1) * MIN_FIXED throws', async () => {
const [a, b] = [-1, MIN_FIXED_VALUE];
const expectedError = new FixedMathRevertErrors.BinOpError(
FixedMathRevertErrors.BinOpErrorCodes.MultiplicationOverflow,
a,
b,
);
const tx = testContract.mul.callAsync(new BigNumber(a), b);
return expect(tx).to.revertWith(expectedError);
});
it('MAX_FIXED * int(-1) == -MAX_FIXED / FIXED_1', async () => {
const [a, b] = [MAX_FIXED_VALUE, -1];
const r = await testContract.mul.callAsync(a, new BigNumber(b));