President Elector

First Flight #24
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: high
Valid

Improper time check allows premature president selection in `selectPresident`.

Description

The selectPresident() function is designed to select a new president after a specific duration (1460 days). However, the storage variable s_previousVoteEndTimeStamp, which is crucial for tracking when the last vote took place, is not explicitly initialized in the constructor. By default, it holds a value of 0. As a result, when block.timestamp - s_previousVoteEndTimeStamp is evaluated for the very first time, it will be significantly greater than i_presidentalDuration (1460 days), because block.timestamp represents the current Unix timestamp in seconds.

function selectPresident() external {
if (
@> block.timestamp - s_previousVoteEndTimeStamp <= i_presidentalDuration
)
.............

Consequently, the function can be called right after the contract deployment, allowing for an immediate president selection without adhering to the expected 4-year term.

Impact

The president can be selected immediately after the contract deployment, bypassing the intended 4-year waiting period. This compromises the integrity of the election process.

Tools Used

Manual review, vscode

Recommended Mitigation

In the contract's constructor, ensure that the s_previousVoteEndTimeStamp is initialized with the actual timestamp of the most recent past election or the current timestamp to mark the first election. Consider making the following changes:

constructor(address[] memory voters) EIP712("RankedChoice", "1") {
VOTERS = voters;
i_presidentalDuration = 1460 days;
s_currentPresident = msg.sender;
s_voteNumber = 0;
+ s_previousVoteEndTimeStamp = block.timestamp;
}
Updates

Lead Judging Commences

inallhonesty Lead Judge 9 months ago
Submission Judgement Published
Validated
Assigned finding tags:

`s_previousVoteEndTimeStamp` variable not being initialized correctly

Support

FAQs

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