Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Restore legacy behavior for ignoring overflows in duration/timestamp #1010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2024
Merged
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
14 changes: 4 additions & 10 deletions eval/public/structs/cel_proto_wrap_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ class ValueManager {
auto reflection,
cel::well_known_types::GetDurationReflection(message->GetDescriptor()),
_.With(ReturnCelValueError(arena_)));
CEL_ASSIGN_OR_RETURN(auto duration, reflection.ToAbslDuration(*message),
_.With(ReturnCelValueError(arena_)));
return CelValue::CreateDuration(duration);
return ValueFromDuration(reflection.UnsafeToAbslDuration(*message));
}

CelValue ValueFromMessage(const Duration* duration) {
Expand All @@ -256,9 +254,7 @@ class ValueManager {
auto reflection,
cel::well_known_types::GetTimestampReflection(message->GetDescriptor()),
_.With(ReturnCelValueError(arena_)));
CEL_ASSIGN_OR_RETURN(auto time, reflection.ToAbslTime(*message),
_.With(ReturnCelValueError(arena_)));
return CelValue::CreateTimestamp(time);
return ValueFromTimestamp(reflection.UnsafeToAbslTime(*message));
}

static CelValue ValueFromTimestamp(absl::Time timestamp) {
Expand Down Expand Up @@ -742,8 +738,7 @@ google::protobuf::Message* DurationFromValue(const google::protobuf::Message* pr
auto reflection,
cel::well_known_types::GetDurationReflection(message->GetDescriptor()),
_.With(IgnoreErrorAndReturnNullptr()));
CEL_RETURN_IF_ERROR(reflection.SetFromAbslDuration(message, val))
.With(IgnoreErrorAndReturnNullptr());
reflection.UnsafeSetFromAbslDuration(message, val);
return message;
}

Expand Down Expand Up @@ -878,8 +873,7 @@ google::protobuf::Message* TimestampFromValue(const google::protobuf::Message* p
auto reflection,
cel::well_known_types::GetTimestampReflection(message->GetDescriptor()),
_.With(IgnoreErrorAndReturnNullptr()));
CEL_RETURN_IF_ERROR(reflection.SetFromAbslTime(message, val))
.With(IgnoreErrorAndReturnNullptr());
reflection.UnsafeSetFromAbslTime(message, val);
return message;
}

Expand Down
36 changes: 36 additions & 0 deletions internal/well_known_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,17 @@ absl::Status DurationReflection::SetFromAbslDuration(
return absl::OkStatus();
}

void DurationReflection::UnsafeSetFromAbslDuration(
absl::Nonnull<google::protobuf::Message*> message, absl::Duration duration) const {
ABSL_DCHECK(IsInitialized());
ABSL_DCHECK_EQ(message->GetDescriptor(), descriptor_);
int64_t seconds = absl::IDivDuration(duration, absl::Seconds(1), &duration);
int32_t nanos = static_cast<int32_t>(
absl::IDivDuration(duration, absl::Nanoseconds(1), &duration));
SetSeconds(message, seconds);
SetNanos(message, nanos);
}

absl::StatusOr<absl::Duration> DurationReflection::ToAbslDuration(
const google::protobuf::Message& message) const {
ABSL_DCHECK(IsInitialized());
Expand All @@ -1052,6 +1063,15 @@ absl::StatusOr<absl::Duration> DurationReflection::ToAbslDuration(
return absl::Seconds(seconds) + absl::Nanoseconds(nanos);
}

absl::Duration DurationReflection::UnsafeToAbslDuration(
const google::protobuf::Message& message) const {
ABSL_DCHECK(IsInitialized());
ABSL_DCHECK_EQ(message.GetDescriptor(), descriptor_);
int64_t seconds = GetSeconds(message);
int32_t nanos = GetNanos(message);
return absl::Seconds(seconds) + absl::Nanoseconds(nanos);
}

absl::StatusOr<DurationReflection> GetDurationReflection(
absl::Nonnull<const Descriptor*> descriptor) {
DurationReflection reflection;
Expand Down Expand Up @@ -1132,6 +1152,15 @@ absl::Status TimestampReflection::SetFromAbslTime(
return absl::OkStatus();
}

void TimestampReflection::UnsafeSetFromAbslTime(
absl::Nonnull<google::protobuf::Message*> message, absl::Time time) const {
int64_t seconds = absl::ToUnixSeconds(time);
int32_t nanos = static_cast<int32_t>((time - absl::FromUnixSeconds(seconds)) /
absl::Nanoseconds(1));
SetSeconds(message, seconds);
SetNanos(message, nanos);
}

absl::StatusOr<absl::Time> TimestampReflection::ToAbslTime(
const google::protobuf::Message& message) const {
int64_t seconds = GetSeconds(message);
Expand All @@ -1149,6 +1178,13 @@ absl::StatusOr<absl::Time> TimestampReflection::ToAbslTime(
return absl::UnixEpoch() + absl::Seconds(seconds) + absl::Nanoseconds(nanos);
}

absl::Time TimestampReflection::UnsafeToAbslTime(
const google::protobuf::Message& message) const {
int64_t seconds = GetSeconds(message);
int32_t nanos = GetNanos(message);
return absl::UnixEpoch() + absl::Seconds(seconds) + absl::Nanoseconds(nanos);
}

absl::StatusOr<TimestampReflection> GetTimestampReflection(
absl::Nonnull<const Descriptor*> descriptor) {
TimestampReflection reflection;
Expand Down
18 changes: 18 additions & 0 deletions internal/well_known_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,18 @@ class DurationReflection final {
absl::Status SetFromAbslDuration(absl::Nonnull<google::protobuf::Message*> message,
absl::Duration duration) const;

// Converts `absl::Duration` to `google.protobuf.Duration` without performing
// validity checks. Avoid use.
void UnsafeSetFromAbslDuration(absl::Nonnull<google::protobuf::Message*> message,
absl::Duration duration) const;

absl::StatusOr<absl::Duration> ToAbslDuration(
const google::protobuf::Message& message) const;

// Converts `google.protobuf.Duration` to `absl::Duration` without performing
// validity checks. Avoid use.
absl::Duration UnsafeToAbslDuration(const google::protobuf::Message& message) const;

private:
absl::Nullable<const google::protobuf::Descriptor*> descriptor_ = nullptr;
absl::Nullable<const google::protobuf::FieldDescriptor*> seconds_field_ = nullptr;
Expand Down Expand Up @@ -817,9 +826,18 @@ class TimestampReflection final {

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

// Converts `absl::Time` to `google.protobuf.Timestamp` without performing
// validity checks. Avoid use.
absl::Time UnsafeToAbslTime(const google::protobuf::Message& message) const;

absl::Status SetFromAbslTime(absl::Nonnull<google::protobuf::Message*> message,
absl::Time time) const;

// Converts `google.protobuf.Timestamp` to `absl::Time` without performing
// validity checks. Avoid use.
void UnsafeSetFromAbslTime(absl::Nonnull<google::protobuf::Message*> message,
absl::Time time) const;

private:
absl::Nullable<const google::protobuf::Descriptor*> descriptor_ = nullptr;
absl::Nullable<const google::protobuf::FieldDescriptor*> seconds_field_ = nullptr;
Expand Down