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

fix(store): prevent delete out of bounds in spliceDynamicData #3521

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/store/src/StoreCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,8 @@ library StoreCoreInternal {
}

// The start index can't be larger than the previous length of the field
if (startWithinField > previousFieldLength) {
revert IStoreErrors.Store_IndexOutOfBounds(previousFieldLength, startWithinField);
if (startWithinField > previousFieldLength - deleteCount) {
revert IStoreErrors.Store_IndexOutOfBounds(previousFieldLength - deleteCount, startWithinField);
}

// Update the encoded length
Expand Down
19 changes: 18 additions & 1 deletion packages/store/test/StoreCore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ contract StoreCoreTest is Test, StoreMock {
vm.expectRevert(
abi.encodeWithSelector(
IStoreErrors.Store_IndexOutOfBounds.selector,
data.newThirdDataBytes.length,
data.newThirdDataBytes.length - uint40(data.thirdDataForUpdate.length),
uint40(type(uint56).max)
)
);
Expand All @@ -1035,6 +1035,23 @@ contract StoreCoreTest is Test, StoreMock {
uint40(data.thirdDataForUpdate.length),
data.thirdDataForUpdate
);

// startByteIndex + deleteCount must not overflow
vm.expectRevert(
abi.encodeWithSelector(
IStoreErrors.Store_IndexOutOfBounds.selector,
data.newThirdDataBytes.length - 8,
data.newThirdDataBytes.length
)
);
this.spliceDynamicData(
data.tableId,
data.keyTuple,
1,
uint40(data.newThirdDataBytes.length), // set start to end of the field
uint40(8), // delete 8 bytes (after the start index, so after the size of the field)
abi.encodePacked(uint64(1)) // append 8 bytes
);
}

function testAccessEmptyData() public {
Expand Down
Loading