Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions internal/overflow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t> 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<int64_t> CheckedAdd(int64_t x, int64_t y) {
Expand Down Expand Up @@ -201,8 +218,6 @@ absl::StatusOr<uint64_t> CheckedMod(uint64_t x, uint64_t y) {
}

absl::StatusOr<absl::Duration> 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).
Expand All @@ -213,25 +228,26 @@ absl::StatusOr<absl::Duration> 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<absl::Duration> 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<absl::Duration> 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<absl::Time> CheckedAdd(absl::Time t, absl::Duration d) {
Expand Down
24 changes: 24 additions & 0 deletions internal/overflow_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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<CheckedDurationResultTest::ParamType>&
info) { return info.param.test_name; });
Expand Down
Loading