Skip to content

Commit 3d8086d

Browse files
authored
Merge pull request #581 from klkvr/klkvr/stdstorage-optimization
fix(StdStorage): check last read slots first
2 parents 07263d1 + 4b6501b commit 3d8086d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/StdStorage.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ library stdStorageSafe {
123123
if (reads.length == 0) {
124124
revert("stdStorage find(StdStorage): No storage use detected for target.");
125125
} else {
126-
for (uint256 i = 0; i < reads.length; i++) {
126+
for (uint256 i = reads.length; --i >= 0;) {
127127
bytes32 prev = vm.load(who, reads[i]);
128128
if (prev == bytes32(0)) {
129129
emit WARNING_UninitedSlot(who, uint256(reads[i]));

test/StdStorage.t.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ contract StdStorageTest is Test {
345345
assertEq(retVal, vals[i]);
346346
}
347347
}
348+
349+
function testEdgeCaseArray() public {
350+
stdstore.target(address(test)).sig("edgeCaseArray(uint256)").with_key(uint256(0)).checked_write(1);
351+
assertEq(test.edgeCaseArray(0), 1);
352+
}
348353
}
349354

350355
contract StorageTest {
@@ -378,6 +383,9 @@ contract StorageTest {
378383

379384
uint256 randomPacking;
380385

386+
// Array with length matching values of elements.
387+
uint256[] public edgeCaseArray = [3, 3, 3];
388+
381389
constructor() {
382390
basic = UnpackedStruct({a: 1337, b: 1337});
383391

0 commit comments

Comments
 (0)