From 48d68b4b01b4cab515b60e5025284d0081571e80 Mon Sep 17 00:00:00 2001 From: kemrec <25618690+kemrec@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:29:08 +0300 Subject: [PATCH] internal/overflow: return overflow error for out-of-range duration arithmetic CheckedAdd/CheckedSub/CheckedNegation for absl::Duration convert operands with absl::ToInt64Nanoseconds(), which silently saturates to INT64_MIN or INT64_MAX for durations beyond roughly 292 years. CEL durations follow the protobuf range of about +/-10,000 years, so a duration can be a valid CEL value yet fall outside the int64 nanosecond range. The arithmetic then runs on the saturated value and returns a wrong result instead of the overflow error the surrounding comment intends. Add a DurationToNanos() helper that returns an overflow error when a duration is not representable as int64 nanoseconds (lossless round trip), and route the three duration helpers through it. This also fixes the transitive CheckedSub(Time, Duration) path through CheckedNegation. Infinite durations are still rejected as before. Adds regression tests for add, subtract, and negate of an in range but non int64 representable duration. --- internal/overflow.cc | 42 +++++++++++++++++++++++++++------------ internal/overflow_test.cc | 24 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/internal/overflow.cc b/internal/overflow.cc index 79d1f24ad..0c75811d1 100644 --- a/internal/overflow.cc +++ b/internal/overflow.cc @@ -71,6 +71,23 @@ bool IsFinite(absl::Time t) { return t != absl::InfiniteFuture() && t != absl::InfinitePast(); } +// Converts a duration to its value in nanoseconds, returning an overflow error +// when the duration lies outside the int64 nanosecond range. +// +// absl::ToInt64Nanoseconds() silently saturates to INT64_MIN/INT64_MAX for +// durations beyond roughly +-292 years, but CEL durations follow the protobuf +// range of +-10,000 years, so a value may be valid yet not representable as +// int64 nanoseconds. Without this guard the duration arithmetic below would run +// on the saturated value and return an incorrect result instead of the +// conservative overflow error intended here. +absl::StatusOr DurationToNanos(absl::Duration d) { + CEL_RETURN_IF_ERROR(CheckRange(IsFinite(d), "integer overflow")); + const int64_t nanos = absl::ToInt64Nanoseconds(d); + CEL_RETURN_IF_ERROR( + CheckRange(absl::Nanoseconds(nanos) == d, "integer overflow")); + return nanos; +} + } // namespace absl::StatusOr CheckedAdd(int64_t x, int64_t y) { @@ -201,8 +218,6 @@ absl::StatusOr CheckedMod(uint64_t x, uint64_t y) { } absl::StatusOr CheckedAdd(absl::Duration x, absl::Duration y) { - CEL_RETURN_IF_ERROR( - CheckRange(IsFinite(x) && IsFinite(y), "integer overflow")); // absl::Duration can handle +- infinite durations, but the Go time.Duration // implementation caps the durations to those expressible within a single // int64 rather than (seconds int64, nanos int32). @@ -213,25 +228,26 @@ absl::StatusOr CheckedAdd(absl::Duration x, absl::Duration y) { // // Since Go is the more conservative of the implementations and 290 year // durations seem quite reasonable, this code mirrors the conservative - // overflow behavior which would be observed in Go. - CEL_ASSIGN_OR_RETURN(int64_t nanos, CheckedAdd(absl::ToInt64Nanoseconds(x), - absl::ToInt64Nanoseconds(y))); + // overflow behavior which would be observed in Go. DurationToNanos() rejects + // operands outside the int64 nanosecond range so that an overflow error is + // returned rather than a value computed from a saturated conversion. + CEL_ASSIGN_OR_RETURN(int64_t nanos_x, DurationToNanos(x)); + CEL_ASSIGN_OR_RETURN(int64_t nanos_y, DurationToNanos(y)); + CEL_ASSIGN_OR_RETURN(int64_t nanos, CheckedAdd(nanos_x, nanos_y)); return absl::Nanoseconds(nanos); } absl::StatusOr CheckedSub(absl::Duration x, absl::Duration y) { - CEL_RETURN_IF_ERROR( - CheckRange(IsFinite(x) && IsFinite(y), "integer overflow")); - CEL_ASSIGN_OR_RETURN(int64_t nanos, CheckedSub(absl::ToInt64Nanoseconds(x), - absl::ToInt64Nanoseconds(y))); + CEL_ASSIGN_OR_RETURN(int64_t nanos_x, DurationToNanos(x)); + CEL_ASSIGN_OR_RETURN(int64_t nanos_y, DurationToNanos(y)); + CEL_ASSIGN_OR_RETURN(int64_t nanos, CheckedSub(nanos_x, nanos_y)); return absl::Nanoseconds(nanos); } absl::StatusOr CheckedNegation(absl::Duration v) { - CEL_RETURN_IF_ERROR(CheckRange(IsFinite(v), "integer overflow")); - CEL_ASSIGN_OR_RETURN(int64_t nanos, - CheckedNegation(absl::ToInt64Nanoseconds(v))); - return absl::Nanoseconds(nanos); + CEL_ASSIGN_OR_RETURN(int64_t nanos, DurationToNanos(v)); + CEL_ASSIGN_OR_RETURN(int64_t negated, CheckedNegation(nanos)); + return absl::Nanoseconds(negated); } absl::StatusOr CheckedAdd(absl::Time t, absl::Duration d) { diff --git a/internal/overflow_test.cc b/internal/overflow_test.cc index 213e7a79d..5ca74b4d3 100644 --- a/internal/overflow_test.cc +++ b/internal/overflow_test.cc @@ -396,6 +396,16 @@ INSTANTIATE_TEST_SUITE_P( return CheckedAdd(absl::Nanoseconds(1), -absl::InfiniteDuration()); }, absl::OutOfRangeError("integer overflow")}, + {"LargeDurationAddNegLargeDuration", + [] { + // ~317 years: a valid CEL duration (within the +-10,000 year + // protobuf range) but outside the int64 nanosecond range. The result + // must be an overflow error, not a value derived from a saturated + // absl::ToInt64Nanoseconds() conversion. + return CheckedAdd(absl::Seconds(int64_t{10000000000}), + absl::Seconds(int64_t{-10000000000})); + }, + absl::OutOfRangeError("integer overflow")}, // Subtraction tests for duration - duration. {"OneSecondSubOneSecond", @@ -428,6 +438,13 @@ INSTANTIATE_TEST_SUITE_P( return CheckedSub(absl::Nanoseconds(1), -absl::InfiniteDuration()); }, absl::OutOfRangeError("integer overflow")}, + {"LargeDurationSubOneSecond", + [] { + // ~317 year operand is outside the int64 nanosecond range. + return CheckedSub(absl::Seconds(int64_t{10000000000}), + absl::Seconds(1)); + }, + absl::OutOfRangeError("integer overflow")}, // Subtraction tests for time - time. {"TimeSubOneSecond", @@ -492,6 +509,13 @@ INSTANTIATE_TEST_SUITE_P( {"NegateNegInfiniteDuration", [] { return CheckedNegation(-absl::InfiniteDuration()); }, absl::OutOfRangeError("integer overflow")}, + {"NegateLargeDuration", + [] { + // ~317 year operand is outside the int64 nanosecond range; negation + // must error rather than operate on a saturated conversion. + return CheckedNegation(absl::Seconds(int64_t{10000000000})); + }, + absl::OutOfRangeError("integer overflow")}, }), [](const testing::TestParamInfo& info) { return info.param.test_name; });