You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
Allarious - [Medium][Gas/Stack Management] Recursive functions are used regularly and can increase gas usage quadratically or might face stack too deep
#73
Closed
sherlock-admin opened this issue
Mar 9, 2023
· 0 comments
sherlock-admin opened this issue
Mar 9, 2023
· 0 comments
Labels
DuplicateA valid issue that is a duplicate of an issue with `Has Duplicates` labelMediumA valid Medium severity issueRewardA payout will be made for this issue
[Medium][Gas/Stack Management] Recursive functions are used regularly and can increase gas usage quadratically or might face stack too deep
Summary
Recursive functions are used throughout the code, while these are a good way to handle certain situations, they can lead to quadratically increasing gas usage if used carelessly.
Vulnerability Detail
In solidity, gas usage of memory allocation is increased quadratically by design, this is to avoid using extreme amounts of memory to do certain tasks. However, throughout the code, there are many recursive functions which recursively use memory to achieve an answer. However, it is a good idea to consider memory and stack uses on various calls, as extensive use of these resources can make the functions uncallable in certain states.
In the code snippet below, we created 6 topHats and one child hat for each, we connected each tree to each other, and ran the function getAdminAtLevel(hatId, 0) to see how gas effects each run. The output for each adding level was as below:
1594
3466
6025
9280
13231
17875
While the increase from first to second level was 1872, the increase in the last two levels was 4644! This value can increase much more and make getAdminAtLevel(hatId, 0) and the functions that use in uncallable.
While this is only one example of the functions, many other functions are also using recursive algorithms that can increase the gas and stack usage.
Another danger is the error stack too deep, it should be noted that solidity only has a stack which is 1024 words deep. using more than that can cause errors.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
DuplicateA valid issue that is a duplicate of an issue with `Has Duplicates` labelMediumA valid Medium severity issueRewardA payout will be made for this issue
Allarious
medium
[Medium][Gas/Stack Management] Recursive functions are used regularly and can increase gas usage quadratically or might face stack too deep
Summary
Recursive functions are used throughout the code, while these are a good way to handle certain situations, they can lead to quadratically increasing gas usage if used carelessly.
Vulnerability Detail
In solidity, gas usage of memory allocation is increased quadratically by design, this is to avoid using extreme amounts of memory to do certain tasks. However, throughout the code, there are many recursive functions which recursively use memory to achieve an answer. However, it is a good idea to consider memory and stack uses on various calls, as extensive use of these resources can make the functions uncallable in certain states.
In the code snippet below, we created 6
topHat
s and one child hat for each, we connected each tree to each other, and ran the functiongetAdminAtLevel(hatId, 0)
to see how gas effects each run. The output for each adding level was as below:While the increase from first to second level was 1872, the increase in the last two levels was 4644! This value can increase much more and make
getAdminAtLevel(hatId, 0)
and the functions that use in uncallable.While this is only one example of the functions, many other functions are also using recursive algorithms that can increase the gas and stack usage.
Another danger is the error
stack too deep
, it should be noted that solidity only has a stack which is 1024 words deep. using more than that can cause errors.Impact
Recursive functions can become uncallable.
https://github.com/Hats-Protocol/hats-protocol/blob/fafcfdf046c0369c1f9e077eacd94a328f9d7af0/src/HatsIdUtilities.sol#L151-L160
Code Snippet
Tool used
Manual Review
Recommendation
Use the recursive functions carefully, it is recommended by the solidity documents to use loops instead of recursive functions as much as possible.
https://docs.soliditylang.org/en/v0.8.19/introduction-to-smart-contracts.html
Duplicate of #96
The text was updated successfully, but these errors were encountered: