-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
226 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.9; | ||
|
||
library Array { | ||
function push(uint256[] memory _nums, uint256 _num) internal pure { | ||
assembly { | ||
// 在可变数组的 末尾追加 一个value (_num) | ||
mstore(add(_nums, mul(add(mload(_nums), 1), 0x20)), _num) | ||
// 可变数组的length 加 1 | ||
mstore(_nums, add(mload(_nums), 1)) | ||
// 0x40 是空闲内存指针的预定义位置 (value 为 空闲指针开始位) | ||
mstore(0x40, add(mload(0x40), 0x20)) | ||
} | ||
} | ||
|
||
function pop(uint256[] memory _nums) internal pure returns (uint256 num_) { | ||
assembly { | ||
// 取出可变数组的最后一个value | ||
num_ := mload(add(_nums, mul(mload(_nums), 0x20))) | ||
// length - 1 | ||
mstore(_nums, sub(mload(_nums), 1)) | ||
} | ||
} | ||
|
||
function del(uint256[] memory _nums, uint256 _index) internal pure { | ||
assembly { | ||
// 下标位置需 小于 数组.length | ||
if lt(_index, mload(_nums)) { | ||
// 将最后一个value 移到 _index 下标处 | ||
mstore(add(_nums, mul(add(_index, 1), 0x20)), mload(add(_nums, mul(mload(_nums), 0x20)))) | ||
// length - 1 | ||
mstore(_nums, sub(mload(_nums), 1)) | ||
} | ||
} | ||
} | ||
|
||
function update(uint256[] memory _nums, uint256 _index, uint256 _num) internal pure { | ||
_nums[_index] = _num; | ||
} | ||
|
||
function get(uint256[] memory _nums, uint256 _index) internal pure returns (uint256) { | ||
return _nums[_index]; | ||
} | ||
} |
131 changes: 96 additions & 35 deletions
131
foundry/test/CTF/Damn-Vulnerable-DeFi/10.Free-Rider.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters