diff --git a/src/shred.cpp b/src/shred.cpp index bc5ba06..2550e06 100644 --- a/src/shred.cpp +++ b/src/shred.cpp @@ -196,9 +196,22 @@ void Shred::performExploration(Drive *drive) size_t savedChunkSize = currentChunkSize; // Generate random chunk size between MIN and MAX (aligned to STEP boundaries) - size_t numSteps = (CHUNK_SIZE_MAX - CHUNK_SIZE_MIN) / CHUNK_SIZE_STEP_UP; - size_t randomStep = rand() % (numSteps + 1); - explorationChunkSize = CHUNK_SIZE_MIN + (randomStep * CHUNK_SIZE_STEP_UP); + // Calculate in MB to avoid overflow + size_t minMB = CHUNK_SIZE_MIN / (1024 * 1024); + size_t maxMB = CHUNK_SIZE_MAX / (1024 * 1024); + size_t stepMB = CHUNK_SIZE_STEP_UP / (1024 * 1024); + + // Number of possible steps: (max - min) / step + size_t numSteps = (maxMB - minMB) / stepMB; + + // Generate random step: 0 to numSteps (inclusive) + // Using proper modulo to ensure range [0, numSteps] + int randVal = rand(); + size_t randomStep = static_cast(randVal) % (numSteps + 1); + + // Calculate exploration chunk size in MB, then convert to bytes + size_t explorationMB = minMB + (randomStep * stepMB); + explorationChunkSize = explorationMB * 1024 * 1024; // Clamp to valid range (safety check) if (explorationChunkSize < CHUNK_SIZE_MIN) @@ -213,7 +226,8 @@ void Shred::performExploration(Drive *drive) // Enhanced logging with debug info Logger::logThis()->info("EXPLORATION MODE: Testing " + to_string(explorationChunkSize / (1024 * 1024)) + " MB chunks " + - "(randomStep=" + to_string(randomStep) + "/" + to_string(numSteps) + ", " + + "(randomStep=" + to_string(randomStep) + "/" + to_string(numSteps) + + ", rand=" + to_string(randVal) + ", " + "was " + to_string(savedChunkSize / (1024 * 1024)) + " MB, " + "best: " + to_string(bestChunkSize / (1024 * 1024)) + " MB)" + " - Drive: " + drive->getSerial());