Skip to content

Commit 8744d3f

Browse files
jckingcopybara-github
authored andcommitted
Restore legacy behavior for ignoring overflows in duration/timestamp
PiperOrigin-RevId: 689184802
1 parent 7a9af94 commit 8744d3f

3 files changed

Lines changed: 58 additions & 10 deletions

File tree

eval/public/structs/cel_proto_wrap_util.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,7 @@ class ValueManager {
242242
auto reflection,
243243
cel::well_known_types::GetDurationReflection(message->GetDescriptor()),
244244
_.With(ReturnCelValueError(arena_)));
245-
CEL_ASSIGN_OR_RETURN(auto duration, reflection.ToAbslDuration(*message),
246-
_.With(ReturnCelValueError(arena_)));
247-
return CelValue::CreateDuration(duration);
245+
return ValueFromDuration(reflection.UnsafeToAbslDuration(*message));
248246
}
249247

250248
CelValue ValueFromMessage(const Duration* duration) {
@@ -256,9 +254,7 @@ class ValueManager {
256254
auto reflection,
257255
cel::well_known_types::GetTimestampReflection(message->GetDescriptor()),
258256
_.With(ReturnCelValueError(arena_)));
259-
CEL_ASSIGN_OR_RETURN(auto time, reflection.ToAbslTime(*message),
260-
_.With(ReturnCelValueError(arena_)));
261-
return CelValue::CreateTimestamp(time);
257+
return ValueFromTimestamp(reflection.UnsafeToAbslTime(*message));
262258
}
263259

264260
static CelValue ValueFromTimestamp(absl::Time timestamp) {
@@ -742,8 +738,7 @@ google::protobuf::Message* DurationFromValue(const google::protobuf::Message* pr
742738
auto reflection,
743739
cel::well_known_types::GetDurationReflection(message->GetDescriptor()),
744740
_.With(IgnoreErrorAndReturnNullptr()));
745-
CEL_RETURN_IF_ERROR(reflection.SetFromAbslDuration(message, val))
746-
.With(IgnoreErrorAndReturnNullptr());
741+
reflection.UnsafeSetFromAbslDuration(message, val);
747742
return message;
748743
}
749744

@@ -878,8 +873,7 @@ google::protobuf::Message* TimestampFromValue(const google::protobuf::Message* p
878873
auto reflection,
879874
cel::well_known_types::GetTimestampReflection(message->GetDescriptor()),
880875
_.With(IgnoreErrorAndReturnNullptr()));
881-
CEL_RETURN_IF_ERROR(reflection.SetFromAbslTime(message, val))
882-
.With(IgnoreErrorAndReturnNullptr());
876+
reflection.UnsafeSetFromAbslTime(message, val);
883877
return message;
884878
}
885879

internal/well_known_types.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,17 @@ absl::Status DurationReflection::SetFromAbslDuration(
10291029
return absl::OkStatus();
10301030
}
10311031

1032+
void DurationReflection::UnsafeSetFromAbslDuration(
1033+
absl::Nonnull<google::protobuf::Message*> message, absl::Duration duration) const {
1034+
ABSL_DCHECK(IsInitialized());
1035+
ABSL_DCHECK_EQ(message->GetDescriptor(), descriptor_);
1036+
int64_t seconds = absl::IDivDuration(duration, absl::Seconds(1), &duration);
1037+
int32_t nanos = static_cast<int32_t>(
1038+
absl::IDivDuration(duration, absl::Nanoseconds(1), &duration));
1039+
SetSeconds(message, seconds);
1040+
SetNanos(message, nanos);
1041+
}
1042+
10321043
absl::StatusOr<absl::Duration> DurationReflection::ToAbslDuration(
10331044
const google::protobuf::Message& message) const {
10341045
ABSL_DCHECK(IsInitialized());
@@ -1052,6 +1063,15 @@ absl::StatusOr<absl::Duration> DurationReflection::ToAbslDuration(
10521063
return absl::Seconds(seconds) + absl::Nanoseconds(nanos);
10531064
}
10541065

1066+
absl::Duration DurationReflection::UnsafeToAbslDuration(
1067+
const google::protobuf::Message& message) const {
1068+
ABSL_DCHECK(IsInitialized());
1069+
ABSL_DCHECK_EQ(message.GetDescriptor(), descriptor_);
1070+
int64_t seconds = GetSeconds(message);
1071+
int32_t nanos = GetNanos(message);
1072+
return absl::Seconds(seconds) + absl::Nanoseconds(nanos);
1073+
}
1074+
10551075
absl::StatusOr<DurationReflection> GetDurationReflection(
10561076
absl::Nonnull<const Descriptor*> descriptor) {
10571077
DurationReflection reflection;
@@ -1132,6 +1152,15 @@ absl::Status TimestampReflection::SetFromAbslTime(
11321152
return absl::OkStatus();
11331153
}
11341154

1155+
void TimestampReflection::UnsafeSetFromAbslTime(
1156+
absl::Nonnull<google::protobuf::Message*> message, absl::Time time) const {
1157+
int64_t seconds = absl::ToUnixSeconds(time);
1158+
int32_t nanos = static_cast<int32_t>((time - absl::FromUnixSeconds(seconds)) /
1159+
absl::Nanoseconds(1));
1160+
SetSeconds(message, seconds);
1161+
SetNanos(message, nanos);
1162+
}
1163+
11351164
absl::StatusOr<absl::Time> TimestampReflection::ToAbslTime(
11361165
const google::protobuf::Message& message) const {
11371166
int64_t seconds = GetSeconds(message);
@@ -1149,6 +1178,13 @@ absl::StatusOr<absl::Time> TimestampReflection::ToAbslTime(
11491178
return absl::UnixEpoch() + absl::Seconds(seconds) + absl::Nanoseconds(nanos);
11501179
}
11511180

1181+
absl::Time TimestampReflection::UnsafeToAbslTime(
1182+
const google::protobuf::Message& message) const {
1183+
int64_t seconds = GetSeconds(message);
1184+
int32_t nanos = GetNanos(message);
1185+
return absl::UnixEpoch() + absl::Seconds(seconds) + absl::Nanoseconds(nanos);
1186+
}
1187+
11521188
absl::StatusOr<TimestampReflection> GetTimestampReflection(
11531189
absl::Nonnull<const Descriptor*> descriptor) {
11541190
TimestampReflection reflection;

internal/well_known_types.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,18 @@ class DurationReflection final {
754754
absl::Status SetFromAbslDuration(absl::Nonnull<google::protobuf::Message*> message,
755755
absl::Duration duration) const;
756756

757+
// Converts `absl::Duration` to `google.protobuf.Duration` without performing
758+
// validity checks. Avoid use.
759+
void UnsafeSetFromAbslDuration(absl::Nonnull<google::protobuf::Message*> message,
760+
absl::Duration duration) const;
761+
757762
absl::StatusOr<absl::Duration> ToAbslDuration(
758763
const google::protobuf::Message& message) const;
759764

765+
// Converts `google.protobuf.Duration` to `absl::Duration` without performing
766+
// validity checks. Avoid use.
767+
absl::Duration UnsafeToAbslDuration(const google::protobuf::Message& message) const;
768+
760769
private:
761770
absl::Nullable<const google::protobuf::Descriptor*> descriptor_ = nullptr;
762771
absl::Nullable<const google::protobuf::FieldDescriptor*> seconds_field_ = nullptr;
@@ -817,9 +826,18 @@ class TimestampReflection final {
817826

818827
absl::StatusOr<absl::Time> ToAbslTime(const google::protobuf::Message& message) const;
819828

829+
// Converts `absl::Time` to `google.protobuf.Timestamp` without performing
830+
// validity checks. Avoid use.
831+
absl::Time UnsafeToAbslTime(const google::protobuf::Message& message) const;
832+
820833
absl::Status SetFromAbslTime(absl::Nonnull<google::protobuf::Message*> message,
821834
absl::Time time) const;
822835

836+
// Converts `google.protobuf.Timestamp` to `absl::Time` without performing
837+
// validity checks. Avoid use.
838+
void UnsafeSetFromAbslTime(absl::Nonnull<google::protobuf::Message*> message,
839+
absl::Time time) const;
840+
823841
private:
824842
absl::Nullable<const google::protobuf::Descriptor*> descriptor_ = nullptr;
825843
absl::Nullable<const google::protobuf::FieldDescriptor*> seconds_field_ = nullptr;

0 commit comments

Comments
 (0)