Skip to content

Commit

Permalink
Fix warning and prevent out-of-bounds access
Browse files Browse the repository at this point in the history
  • Loading branch information
philippwerner committed Mar 13, 2024
1 parent b9777cc commit 47ffc51
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/qubic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,10 @@ static void processBroadcastMessage(const unsigned long long processorNumber, Re
}
if (k == system.numberOfSolutions)
{
unsigned int solutionScore = (*score)(processorNumber, request->destinationPublicKey, solution_nonce);
unsigned long long solutionScore = (*score)(processorNumber, request->destinationPublicKey, solution_nonce);
const int threshold = (system.epoch < MAX_NUMBER_EPOCH) ? solutionThreshold[system.epoch] : SOLUTION_THRESHOLD_DEFAULT;
if (system.numberOfSolutions < MAX_NUMBER_OF_SOLUTIONS
&& ((solutionScore >= (DATA_LENGTH / 2) + solutionThreshold[system.epoch]) || (solutionScore <= (DATA_LENGTH / 2) - solutionThreshold[system.epoch])))
&& ((solutionScore >= (DATA_LENGTH / 2) + threshold) || (solutionScore <= (DATA_LENGTH / 2) - threshold)))
{
ACQUIRE(solutionsLock);

Expand Down Expand Up @@ -1126,14 +1127,14 @@ static void processSpecialCommand(Peer* peer, RequestResponseHeader* header)
{
SpecialCommandSetSolutionThresholdRequestAndResponse* _request = header->getPayload<SpecialCommandSetSolutionThresholdRequestAndResponse>();
// can only set future epoch
if (_request->epoch > system.epoch)
if (_request->epoch > system.epoch && _request->epoch < MAX_NUMBER_EPOCH)
{
solutionThreshold[_request->epoch] = _request->threshold;
}
SpecialCommandSetSolutionThresholdRequestAndResponse response;
response.everIncreasingNonceAndCommandType = _request->everIncreasingNonceAndCommandType;
response.epoch = _request->epoch;
response.threshold = solutionThreshold[_request->epoch];
response.threshold = (_request->epoch < MAX_NUMBER_EPOCH) ? solutionThreshold[_request->epoch] : SOLUTION_THRESHOLD_DEFAULT;
enqueueResponse(peer, sizeof(SpecialCommandSetSolutionThresholdRequestAndResponse), SpecialCommand::type, header->dejavu(), &response);
}
break;
Expand Down Expand Up @@ -2117,7 +2118,8 @@ static void processTick(unsigned long long processorNumber)
resourceTestingDigest ^= solutionScore;
KangarooTwelve(&resourceTestingDigest, sizeof(resourceTestingDigest), &resourceTestingDigest, sizeof(resourceTestingDigest));

if (((solutionScore >= (DATA_LENGTH / 2) + solutionThreshold[system.epoch]) || (solutionScore <= (DATA_LENGTH / 2) - solutionThreshold[system.epoch])))
const int threshold = (system.epoch < MAX_NUMBER_EPOCH) ? solutionThreshold[system.epoch] : SOLUTION_THRESHOLD_DEFAULT;
if (((solutionScore >= (DATA_LENGTH / 2) + threshold) || (solutionScore <= (DATA_LENGTH / 2) - threshold)))
{
for (unsigned int i = 0; i < sizeof(computorSeeds) / sizeof(computorSeeds[0]); i++)
{
Expand Down Expand Up @@ -2532,7 +2534,7 @@ static void beginEpoch1of2()
minimumComputorScore = 0;
minimumCandidateScore = 0;

if (solutionThreshold[system.epoch] <= 0 || solutionThreshold[system.epoch] > DATA_LENGTH) { // invalid threshold
if (system.epoch < MAX_NUMBER_EPOCH && (solutionThreshold[system.epoch] <= 0 || solutionThreshold[system.epoch] > DATA_LENGTH)) { // invalid threshold
solutionThreshold[system.epoch] = SOLUTION_THRESHOLD_DEFAULT;
}

Expand Down

0 comments on commit 47ffc51

Please sign in to comment.