Skip to content

Commit

Permalink
Merge pull request #308 from primitivefinance/audit-fix/toBytes8/16
Browse files Browse the repository at this point in the history
Audit fix/to bytes8/16
  • Loading branch information
clemlak authored Mar 31, 2023
2 parents 7cafa4e + c9ba41c commit 9937617
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions contracts/libraries/AssemblyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.13;

error InvalidLiquidity();
error InvalidDays();
error DataTooLong();

uint256 constant SECONDS_PER_YEAR = 31556953 seconds;
uint256 constant SECONDS_PER_DAY = 86_400 seconds;
Expand Down Expand Up @@ -154,7 +155,14 @@ library AssemblyLib {
* handles it for us.
*/
function toBytes16(bytes memory raw) internal pure returns (bytes16 data) {
bytes4 errorSelector = DataTooLong.selector;

assembly {
if gt(mload(raw), 16) {
mstore(0, errorSelector)
revert(0, 4)
}

data := mload(add(raw, 32))
let shift := mul(sub(16, mload(raw)), 8)
data := shr(shift, data)
Expand All @@ -166,7 +174,14 @@ library AssemblyLib {
* handles it for us.
*/
function toBytes8(bytes memory raw) internal pure returns (bytes8 data) {
bytes4 errorSelector = DataTooLong.selector;

assembly {
if gt(mload(raw), 8) {
mstore(0, errorSelector)
revert(0, 4)
}

data := mload(add(raw, 32))
let shift := mul(sub(8, mload(raw)), 8)
data := shr(shift, data)
Expand Down
40 changes: 40 additions & 0 deletions test/TestAssemblyLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,46 @@ contract TestAssemblyLib is Test {
}
*/

function test_toBytes16() public {
bytes memory input = hex"1234567890abcdef1234567890abcdef";
bytes16 expectedOutput = hex"1234567890abcdef1234567890abcdef";
bytes16 output = AssemblyLib.toBytes16(input);
assertEq(output, expectedOutput);
}

function test_toBytes16_RightPad() public {
bytes memory input = hex"01";
bytes16 expectedOutput = hex"00000000000000000000000000000001";
bytes16 output = AssemblyLib.toBytes16(input);
assertEq(output, expectedOutput);
}

function test_toBytes16_RevertIfLengthTooLong() public {
bytes memory input = hex"1234567890abcdef1234567890abcdef00";
vm.expectRevert(DataTooLong.selector);
AssemblyLib.toBytes16(input);
}

function test_toBytes8() public {
bytes memory input = hex"1234567890abcdef";
bytes16 expectedOutput = hex"1234567890abcdef";
bytes16 output = AssemblyLib.toBytes8(input);
assertEq(output, expectedOutput);
}

function test_toBytes8_RightPad() public {
bytes memory input = hex"01";
bytes16 expectedOutput = hex"0000000000000001";
bytes16 output = AssemblyLib.toBytes8(input);
assertEq(output, expectedOutput);
}

function test_toBytes8_RevertIfLengthTooLong() public {
bytes memory input = hex"1234567890abcdef00";
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

0 comments on commit 9937617

Please sign in to comment.