From fdba441d8b9a5393ec4f31b0cbfe66d33ec0123b Mon Sep 17 00:00:00 2001 From: Joonas Lipping Date: Fri, 16 Oct 2026 08:11:44 +0300 Subject: [PATCH] fix(optimization): prevent integer overflow when computing oca stop time Adding a large max_runtime duration to steady_clock::now() can overflow on platforms where the clock epoch is large (observed on macOS with GCC), wrapping time_to_stop to a value in the past and causing an immediate early exit. Fix the computation site: clamp time_to_stop to time_point::max() if the addition would overflow, rather than capping the default duration. --- dlib/optimization/optimization_oca.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dlib/optimization/optimization_oca.h b/dlib/optimization/optimization_oca.h index 8d10f9a828..f475870304 100644 --- a/dlib/optimization/optimization_oca.h +++ b/dlib/optimization/optimization_oca.h @@ -264,7 +264,10 @@ namespace dlib const double prior_norm = have_prior ? 0.5*dot(prior,prior) : 0; - const auto time_to_stop = std::chrono::steady_clock::now() + max_runtime; + const auto now = std::chrono::steady_clock::now(); + const auto time_to_stop = (max_runtime > std::chrono::steady_clock::time_point::max() - now) + ? std::chrono::steady_clock::time_point::max() + : now + max_runtime; unsigned long counter = 0; while (true)