From 168f9cd9e417c1f79831bd2681e90f6c2a5c16c4 Mon Sep 17 00:00:00 2001 From: Ran Trifon Date: Tue, 22 Sep 2026 20:27:39 +0300 Subject: [PATCH] Fix overflow when checking large native operation timeouts --- CHANGELOG.md | 4 ++++ src/cpp/src/operation_budget.hpp | 6 +++++- tests/cpp/atomic_budget_tests.cpp | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d1ca2..ceefdc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ chronological order. ## Unreleased +- Fixed native finite operation timeouts above the clock's nanosecond range + expiring immediately. This is a patch-level correction with no API or mapped + protocol change. + ## 3.0.0 - 2026-07-16 ### Breaking changes diff --git a/src/cpp/src/operation_budget.hpp b/src/cpp/src/operation_budget.hpp index c7d10a8..277aff2 100644 --- a/src/cpp/src/operation_budget.hpp +++ b/src/cpp/src/operation_budget.hpp @@ -73,7 +73,11 @@ class OperationBudget { } if (!valid()) return SMS_STATUS_UNKNOWN_FAILURE; if (is_infinite() || is_no_wait()) return SMS_STATUS_SUCCESS; - return clock::now() - started_ >= timeout_ + // Compare in the public timeout unit. Converting an accepted int64 + // millisecond timeout to the clock's finer ticks can overflow. + const auto elapsed = std::chrono::duration_cast( + clock::now() - started_); + return elapsed >= timeout_ ? SMS_STATUS_STORE_BUSY : SMS_STATUS_SUCCESS; } diff --git a/tests/cpp/atomic_budget_tests.cpp b/tests/cpp/atomic_budget_tests.cpp index aebc45f..d2fb287 100644 --- a/tests/cpp/atomic_budget_tests.cpp +++ b/tests/cpp/atomic_budget_tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include static_assert(sizeof(void*) == 8); @@ -85,6 +86,27 @@ int main() { SMS_CHECK(finite.try_continue_after_contention(0, terminal)); SMS_CHECK(terminal == SMS_STATUS_SUCCESS); + // These finite millisecond values exceed the signed nanosecond range. + // Checking a fresh budget must not multiply the timeout into overflow. + for (const auto timeout : { + std::chrono::milliseconds{9'223'372'036'854LL}, + std::chrono::milliseconds{9'223'372'036'855LL}, + std::chrono::milliseconds{std::numeric_limits::max()}}) { + auto large = OperationBudget::start_at(timeout, now); + SMS_CHECK(large.valid()); + SMS_CHECK(!large.is_infinite()); + SMS_CHECK(large.check() == SMS_STATUS_SUCCESS); + SMS_CHECK(large.check_periodic(64) == SMS_STATUS_SUCCESS); + terminal = SMS_STATUS_UNKNOWN_FAILURE; + SMS_CHECK(large.try_continue_after_contention(0, terminal)); + SMS_CHECK(terminal == SMS_STATUS_SUCCESS); + + CancellationFlag large_cancellation; + auto cancelable_large = OperationBudget::start_at(timeout, now, &large_cancellation); + large_cancellation.cancel(); + SMS_CHECK(cancelable_large.check() == SMS_STATUS_OPERATION_CANCELED); + } + auto expired = OperationBudget::start_at(1ms, now - 10ms); SMS_CHECK(expired.check() == SMS_STATUS_STORE_BUSY); SMS_CHECK(expired.check_periodic(64) == SMS_STATUS_STORE_BUSY);