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

feat: remediations #6

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion src/SentinelList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
self.entries[SENTINEL] = newEntry;
}

function safePush(SentinelList storage self, address newEntry) internal {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes L4

if (!alreadyInitialized({ self: self })) {
init({ self: self });
}
push({ self: self, newEntry: newEntry });
}

function pop(SentinelList storage self, address prevEntry, address popEntry) internal {
if (popEntry == ZERO_ADDRESS || popEntry == SENTINEL) {
revert LinkedList_InvalidEntry(prevEntry);
Expand All @@ -55,7 +62,6 @@
next = self.entries[next];
self.entries[current] = ZERO_ADDRESS;
}
self.entries[SENTINEL] = ZERO_ADDRESS;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes I7

}

function contains(SentinelList storage self, address entry) internal view returns (bool) {
Expand Down Expand Up @@ -104,7 +110,7 @@
// Set correct size of returned array
// solhint-disable-next-line no-inline-assembly
/// @solidity memory-safe-assembly
assembly {

Check warning on line 113 in src/SentinelList.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

Avoid to use inline assembly. It is acceptable only in rare cases
mstore(array, entryCount)
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/SentinelList4337.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
self.entries[SENTINEL][account] = newEntry;
}

function safePush(SentinelList storage self, address account, address newEntry) internal {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes L4

if (!alreadyInitialized(self, account)) {
init({ self: self, account: account });
}
push({ self: self, account: account, newEntry: newEntry });
}

function pop(
SentinelList storage self,
address account,
Expand All @@ -86,7 +93,6 @@
next = self.entries[next][account];
self.entries[current][account] = ZERO_ADDRESS;
}
self.entries[SENTINEL][account] = ZERO_ADDRESS;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes I7

}

function contains(
Expand Down Expand Up @@ -146,7 +152,7 @@
// Set correct size of returned array
// solhint-disable-next-line no-inline-assembly
/// @solidity memory-safe-assembly
assembly {

Check warning on line 155 in src/SentinelList4337.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

Avoid to use inline assembly. It is acceptable only in rare cases
mstore(array, entryCount)
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/SentinelListBytes32.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
self.entries[SENTINEL] = newEntry;
}

function safePush(LinkedBytes32 storage self, bytes32 newEntry) internal {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes L4

if (!alreadyInitialized({ self: self })) {
init({ self: self });
}
push({ self: self, newEntry: newEntry });
}

function pop(LinkedBytes32 storage self, bytes32 prevEntry, bytes32 popEntry) internal {
if (popEntry == ZERO || popEntry == SENTINEL) {
revert LinkedList_InvalidEntry(prevEntry);
Expand All @@ -55,7 +62,6 @@
next = self.entries[next];
self.entries[current] = ZERO;
}
self.entries[SENTINEL] = ZERO;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes I7

}

function contains(LinkedBytes32 storage self, bytes32 entry) internal view returns (bool) {
Expand All @@ -71,7 +77,7 @@
view
returns (bytes32[] memory array, bytes32 next)
{
if (start != SENTINEL && contains(self, start)) revert LinkedList_InvalidEntry(start);
if (start != SENTINEL && !contains(self, start)) revert LinkedList_InvalidEntry(start);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes L6

if (pageSize == 0) revert LinkedList_InvalidPage();
// Init array with max page size
array = new bytes32[](pageSize);
Expand All @@ -98,13 +104,13 @@
* incSENTINELrent page, nor will it be included in the next one if you pass it as a
* start.
*/
if (next != SENTINEL) {
if (next != SENTINEL && entryCount > 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: fixes underflow bug previously fixed in other flavours of the lib

next = array[entryCount - 1];
}
// Set correct size of returned array
// solhint-disable-next-line no-inline-assembly
/// @solidity memory-safe-assembly
assembly {

Check warning on line 113 in src/SentinelListBytes32.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

Avoid to use inline assembly. It is acceptable only in rare cases
mstore(array, entryCount)
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/concrete/SentinelList.tree
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ SentinelListLib::push
└── it should add the entry to the list


SentinelListLib::safePush
├── when list is not initialized
│ └── it should initialize list
└── when list is initialized
├── when entry is zero
│ └── it should revert
├── when entry is sentinel
│ └── it should revert
└── when entry is not sentinel
├── when entry is already added
│ └── it should revert
└── when entry is not added
└── it should add the entry to the list


SentinelListLib::pop
├── when entry is zero
│ └── it should revert
Expand Down
50 changes: 50 additions & 0 deletions test/concrete/SentinelList4337Test.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,52 @@ contract SentinelList4337Test is Test {
list.push(account, address(newEntry));
}

function test_SafePushWhenListIsNotInitialized() external {
// it should initialize list
address newEntry = makeAddr("newEntry");
newList.safePush(account, newEntry);

assertTrue(newList.alreadyInitialized(account));
}

function test_SafePushRevertWhen_EntryIsZero() external whenListIsInitialized {
// it should revert
vm.expectRevert();
newList.safePush(account, ZERO_ADDRESS);
}

function test_SafePushRevertWhen_EntryIsSentinel() external whenListIsInitialized {
// it should revert
vm.expectRevert();
newList.safePush(account, SENTINEL);
}

function test_SafePushRevertWhen_EntryIsAlreadyAdded()
external
whenListIsInitialized
whenEntryIsNotSentinel
{
// it should revert
address newEntry = makeAddr("newEntry");
newList.safePush(account, address(newEntry));

address next = newList.getNext(account, SENTINEL);
assertEq(next, newEntry);

vm.expectRevert();
newList.safePush(account, address(newEntry));
}

function test_SafePushWhenEntryIsNotAdded()
external
whenListIsInitialized
whenEntryIsNotSentinel
{
// it should add the entry to the list
address newEntry = makeAddr("newEntry");
newList.safePush(account, address(newEntry));
}

function test_PopRevertWhen_EntryIsZero() external {
// it should revert
vm.expectRevert();
Expand Down Expand Up @@ -232,4 +278,8 @@ contract SentinelList4337Test is Test {
modifier whenEntryIsContainedOrSentinel() {
_;
}

modifier whenListIsInitialized() {
_;
}
}
50 changes: 50 additions & 0 deletions test/concrete/SentinelListBytes32Test.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,52 @@ contract SentinelListBytes32Test is Test {
list.push(bytes32(newEntry));
}

function test_SafePushWhenListIsNotInitialized() external {
// it should initialize list
bytes32 newEntry = keccak256("newEntry");
newList.safePush(newEntry);

assertTrue(newList.alreadyInitialized());
}

function test_SafePushRevertWhen_EntryIsZero() external whenListIsInitialized {
// it should revert
vm.expectRevert();
newList.safePush(ZERO);
}

function test_SafePushRevertWhen_EntryIsSentinel() external whenListIsInitialized {
// it should revert
vm.expectRevert();
newList.safePush(SENTINEL);
}

function test_SafePushRevertWhen_EntryIsAlreadyAdded()
external
whenListIsInitialized
whenEntryIsNotSentinel
{
// it should revert
bytes32 newEntry = keccak256("newEntry");
newList.safePush(newEntry);

bytes32 next = newList.getNext(SENTINEL);
assertEq(next, newEntry);

vm.expectRevert();
newList.safePush(newEntry);
}

function test_SafePushWhenEntryIsNotAdded()
external
whenListIsInitialized
whenEntryIsNotSentinel
{
// it should add the entry to the list
bytes32 newEntry = keccak256("newEntry");
newList.safePush(newEntry);
}

function test_PopRevertWhen_EntryIsZero() external {
// it should revert
vm.expectRevert();
Expand Down Expand Up @@ -229,4 +275,8 @@ contract SentinelListBytes32Test is Test {
modifier whenEntryIsContainedOrSentinel() {
_;
}

modifier whenListIsInitialized() {
_;
}
}
50 changes: 50 additions & 0 deletions test/concrete/SentinelListTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,52 @@ contract SentinelListTest is Test {
list.push(address(newEntry));
}

function test_SafePushWhenListIsNotInitialized() external {
// it should initialize list
address newEntry = makeAddr("newEntry");
newList.safePush(newEntry);

assertTrue(newList.alreadyInitialized());
}

function test_SafePushRevertWhen_EntryIsZero() external whenListIsInitialized {
// it should revert
vm.expectRevert();
newList.safePush(ZERO_ADDRESS);
}

function test_SafePushRevertWhen_EntryIsSentinel() external whenListIsInitialized {
// it should revert
vm.expectRevert();
newList.safePush(SENTINEL);
}

function test_SafePushRevertWhen_EntryIsAlreadyAdded()
external
whenListIsInitialized
whenEntryIsNotSentinel
{
// it should revert
address newEntry = makeAddr("newEntry");
newList.safePush(address(newEntry));

address next = newList.getNext(SENTINEL);
assertEq(next, newEntry);

vm.expectRevert();
newList.safePush(address(newEntry));
}

function test_SafePushWhenEntryIsNotAdded()
external
whenListIsInitialized
whenEntryIsNotSentinel
{
// it should add the entry to the list
address newEntry = makeAddr("newEntry");
newList.safePush(address(newEntry));
}

function test_PopRevertWhen_EntryIsZero() external {
// it should revert
vm.expectRevert();
Expand Down Expand Up @@ -229,4 +275,8 @@ contract SentinelListTest is Test {
modifier whenEntryIsContainedOrSentinel() {
_;
}

modifier whenListIsInitialized() {
_;
}
}