Skip to content

Commit c5896d7

Browse files
authored
Merge pull request #119 from meetjn/election-removeCandidate
Bug: added edge case. closes #118
2 parents 259e13b + b173f75 commit c5896d7

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

blockchain/contracts/Election.sol

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ contract Election is Initializable {
1111
error GetVotes();
1212
error ElectionIncomplete();
1313
error ElectionInactive();
14+
error InvalidCandidateID();
1415

1516
mapping(address user => bool isVoted) public userVoted;
1617

@@ -120,10 +121,10 @@ contract Election is Initializable {
120121
}
121122

122123
function removeCandidate(uint _id) external onlyOwner electionStarted {
123-
candidates[_id] = candidates[candidates.length - 1];
124-
candidates[_id].candidateID = _id;
125-
candidates.pop();
126-
}
124+
if (_id >= candidates.length) revert InvalidCandidateID();
125+
candidates[_id] = candidates[candidates.length - 1]; // Replace with last element
126+
candidates.pop();
127+
}
127128

128129
function getCandidateList() external view returns (Candidate[] memory) {
129130
return candidates;

0 commit comments

Comments
 (0)