Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit fix/missing signextend #319

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions contracts/libraries/AssemblyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ library AssemblyLib {
}

/**
* @dev Adds a signed `delta` to an unsigned `input` by using the sign-agnostic 256-bit type used in yul.
* Checks for overflow manually and reverts with `InvalidLiquidity()`, because this function is used to
* change liquidity in a position or pool.
* @dev Adds a signed `delta` to an unsigned `input`.
* @custom:example
* ```
* uint128 output = addSignedDelta(uint128(15), -int128(5));
Expand All @@ -78,14 +76,10 @@ library AssemblyLib {
uint128 input,
int128 delta
) internal pure returns (uint128 output) {
bytes memory revertData =
abi.encodeWithSelector(InvalidLiquidity.selector);
assembly {
output := add(input, delta)
// Reverts on overflow.
if gt(output, 0xffffffffffffffffffffffffffffffff) {
revert(add(32, revertData), mload(revertData))
} // 0x1fff9681
if (delta > 0) {
output = input + uint128(delta);
} else {
output = input - uint128(-delta);
}
}

Expand Down
26 changes: 20 additions & 6 deletions test/TestAssemblyLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,29 @@ contract TestAssemblyLib is Test {
}
}

/*
FIXME: These tests are not working yet.
function testFuzz_addSignedDelta(uint128 input, int128 delta) public {
// If delta is positive but the sum of input and delta is greater than
// the maximum value of uint128, we revert.
if (delta >= 0 && (uint256(input) + uint256(uint128(delta))) > type(uint128).max) {
vm.expectRevert();
}

// If delta is negative but its absolute value is greater than input,
// we revert.
if (
delta == -170141183460469231731687303715884105728
|| delta < 0 && uint128(-delta) > input
) {
vm.expectRevert();
}

uint128 output = AssemblyLib.addSignedDelta(input, delta);

assertEq(
AssemblyLib.addSignedDelta(input, delta),
delta < 0 ? uint128(-delta) : uint128(delta)
output,
delta < 0 ? input - uint128(-delta) : input + uint128(delta)
);
}
*/

function testFuzz_separate(uint8 a, uint8 b) public {
vm.assume(a <= 15);
Expand Down Expand Up @@ -90,7 +104,7 @@ contract TestAssemblyLib is Test {
vm.expectRevert(DataTooLong.selector);
AssemblyLib.toBytes8(input);
}

function test_pack() public {
bytes1 output = AssemblyLib.pack(bytes1(0x01), bytes1(0x02));
assertEq(output, bytes1(0x12));
Expand Down