I was going over the best practices in Defender Advisor and was trying to add pagination to my contract. I was following the code from the article but I think there might be an issue with the code sample.
pragma solidity ^0.6.0;
contract PaginatedStoreQuerier {
function entryAtPaginated(EnumerableStore store, uint256 startIndex, uint256 pageSize) public view returns(uint256[] memory) {
uint256[] memory result;
require(startIndex + pageSize <= store.totalEntries(), "Read index out of bounds");
for (uint256 i = 0; i < pageSize; ++i) {
result[i] = store.entryAt(i + startIndex);
}
return result;
}
}
Here the result
is not initialized, but as far as I know memory arrays are not dynamic. In fact this function fails when I try it in Solidity 0.8.7.
I believe that the result initialization code should look something like this:
uint256[] memory result = new uint256[](pageSize)
But overall thank you for making Defender available to all, this tool is amazing.