Skip to content
Open
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
3 changes: 2 additions & 1 deletion contracts/facets/DiamondLoupeFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ contract DiamondLoupeFacet is IDiamondLoupe, IERC165 {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 numFacets = ds.facetAddresses.length;
facets_ = new Facet[](numFacets);
for (uint256 i; i < numFacets; i++) {
for (uint256 i; i < numFacets;) {
address facetAddress_ = ds.facetAddresses[i];
facets_[i].facetAddress = facetAddress_;
facets_[i].functionSelectors = ds.facetFunctionSelectors[facetAddress_].functionSelectors;
unchecked { ++i; }
}
}

Expand Down
16 changes: 10 additions & 6 deletions contracts/libraries/LibDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ library LibDiamond {
address _init,
bytes memory _calldata
) internal {
for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) {
for (uint256 facetIndex; facetIndex < _diamondCut.length;) {
IDiamondCut.FacetCutAction action = _diamondCut[facetIndex].action;
if (action == IDiamondCut.FacetCutAction.Add) {
addFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
Expand All @@ -83,6 +83,7 @@ library LibDiamond {
} else {
revert("LibDiamondCut: Incorrect FacetCutAction");
}
unchecked { ++facetIndex; }
}
emit DiamondCut(_diamondCut, _init, _calldata);
initializeDiamondCut(_init, _calldata);
Expand All @@ -97,12 +98,13 @@ library LibDiamond {
if (selectorPosition == 0) {
addFacet(ds, _facetAddress);
}
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length;) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
require(oldFacetAddress == address(0), "LibDiamondCut: Can't add function that already exists");
addFunction(ds, selector, selectorPosition, _facetAddress);
selectorPosition++;
++selectorPosition;
unchecked { ++selectorIndex; }
Copy link
Owner

Choose a reason for hiding this comment

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

for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++)

selectorPosition is now being incremented twice in each iteration of the loop, instead of once.

}
}

Expand All @@ -115,13 +117,14 @@ library LibDiamond {
if (selectorPosition == 0) {
addFacet(ds, _facetAddress);
}
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length;) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
require(oldFacetAddress != _facetAddress, "LibDiamondCut: Can't replace function with same function");
removeFunction(ds, oldFacetAddress, selector);
addFunction(ds, selector, selectorPosition, _facetAddress);
selectorPosition++;
++selectorPosition;
unchecked { ++selectorIndex; }
}
}

Expand All @@ -130,10 +133,11 @@ library LibDiamond {
DiamondStorage storage ds = diamondStorage();
// if function does not exist then do nothing and return
require(_facetAddress == address(0), "LibDiamondCut: Remove facet address must be address(0)");
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length;) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
removeFunction(ds, oldFacetAddress, selector);
unchecked { ++selectorIndex; }
}
}

Expand Down