Skip to content

Commit a62d6f4

Browse files
authored
MaterialFarmer: fix std::chrono overflow issue (#639)
1 parent 5bc4223 commit a62d6f4

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ void run_material_farmer(
216216
std::to_string(farming_time_remaining.count()) + " min",
217217
COLOR_PURPLE
218218
);
219-
if (farming_time_remaining < std::chrono::minutes(0)){
219+
if (farming_time_remaining <= std::chrono::minutes(0)){
220220
console.log("Time's up. Stop the Material farming program.", COLOR_RED);
221221
return;
222222
}
@@ -229,7 +229,7 @@ void run_material_farmer(
229229
std::to_string(sandwich_time_remaining.count()) + " min",
230230
COLOR_PURPLE
231231
);
232-
if (sandwich_time_remaining < std::chrono::minutes(0)){
232+
if (sandwich_time_remaining <= std::chrono::minutes(0)){
233233
console.log("Sandwich not active. Make a sandwich.");
234234
last_sandwich_time = make_sandwich_material_farm(env, console, context, options, stats);
235235
console.overlay().add_log("Sandwich made.");
@@ -393,9 +393,14 @@ WallClock make_sandwich_material_farm(
393393
}
394394

395395
// given the start time, and duration in minutes, return the number of remaining minutes
396+
// WARNING: this function may silently overflow if start_time is near WallClock::min() or WallClock::max()
396397
std::chrono::minutes minutes_remaining(WallClock start_time, std::chrono::minutes minutes_duration){
397-
return std::chrono::minutes(minutes_duration) -
398-
std::chrono::duration_cast<std::chrono::minutes>(current_time() - start_time);
398+
if (start_time == WallClock::min()){
399+
return std::chrono::minutes(0);
400+
}else{
401+
auto elapsed_time = std::chrono::duration_cast<std::chrono::minutes>(current_time() - start_time);
402+
return minutes_duration - elapsed_time;
403+
}
399404
}
400405

401406
// from the North Province (Area 3) pokecenter, move to start position for Happiny dust farming

0 commit comments

Comments
 (0)