Hawk High

First Flight #39
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Impact: low
Likelihood: high
Invalid

Gas Inefficiency in Array Removals in LevelOne.sol

Summary

The removeTeacher and expel functions in the LevelOne contract use linear search (O(n)) to find elements in arrays before removing them, which becomes increasingly gas-inefficient as the arrays grow in size.

Vulnerability Details

Both functions iterate through arrays to find target elements:

function removeTeacher(address _teacher) public onlyPrincipal {
// ...
uint256 teacherLength = listOfTeachers.length;
for (uint256 n = 0; n < teacherLength; n++) {
if (listOfTeachers[n] == _teacher) {
listOfTeachers[n] = listOfTeachers[teacherLength - 1];
listOfTeachers.pop();
break;
}
}
// ...
}

Impact

As the school grows and more teachers/students are added, these operations will:

  • Consume increasingly more gas

  • Eventually risk hitting block gas limits with large arrays

  • Make operations prohibitively expensive during high network congestion

  • Potentially cause transaction failures for critical administrative functions

Tools Used

Foundry Forge

Manuel Code review

Recommendations

Implement a mapping-based approach to track array indices:

mapping(address => uint256) private teacherIndices;
function addTeacher(address _teacher) public onlyPrincipal notYetInSession {
// ...
teacherIndices[_teacher] = listOfTeachers.length;
listOfTeachers.push(_teacher);
// ...
}
function removeTeacher(address _teacher) public onlyPrincipal {
// ...
uint256 index = teacherIndices[_teacher];
uint256 lastIndex = listOfTeachers.length - 1;
if (index != lastIndex) {
address lastTeacher = listOfTeachers[lastIndex];
listOfTeachers[index] = lastTeacher;
teacherIndices[lastTeacher] = index;
}
listOfTeachers.pop();
delete teacherIndices[_teacher];
// ...
}
Updates

Lead Judging Commences

yeahchibyke Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Design choice

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.