Skip to content

Commit 33b4b98

Browse files
authored
♻️ Strengthen safeMoveETH (#1474)
1 parent 0f97b01 commit 33b4b98

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/utils/SafeTransferLib.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,16 @@ library SafeTransferLib {
205205
assembly {
206206
to := shr(96, shl(96, to)) // Clean upper 96 bits.
207207
for { let mover := ETH_MOVER } iszero(eq(to, address())) {} {
208-
if or(lt(selfbalance(), amount), eq(to, mover)) {
208+
let selfBalanceBefore := selfbalance()
209+
if or(lt(selfBalanceBefore, amount), eq(to, mover)) {
209210
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
210211
revert(0x1c, 0x04)
211212
}
212213
if extcodesize(mover) {
213214
let balanceBefore := balance(to) // Check via delta, in case `SELFDESTRUCT` is bricked.
214215
pop(call(gas(), mover, amount, codesize(), 0x00, codesize(), 0x00))
215216
if iszero(lt(add(amount, balance(to)), balanceBefore)) { break }
217+
if lt(selfBalanceBefore, selfbalance()) { invalid() } // Just in case.
216218
}
217219
let m := mload(0x40)
218220
// If the mover is missing or bricked, deploy a minimal vault

test/SafeTransferLib.t.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,8 @@ contract SafeTransferLibTest is SoladyTest {
12671267
}
12681268

12691269
function safeMoveETH(address to, uint256 amount) public returns (address) {
1270+
if (_randomChance(2)) _misalignFreeMemoryPointer();
1271+
if (_randomChance(2)) _brutalizeMemory();
12701272
return SafeTransferLib.safeMoveETH(_brutalized(to), amount);
12711273
}
12721274

0 commit comments

Comments
 (0)