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
56 changes: 56 additions & 0 deletions common/values/error_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,62 @@ ErrorValue IndexOutOfBoundsError(ptrdiff_t index) {
absl::InvalidArgumentError(absl::StrCat("index out of bounds: ", index)));
}

ErrorValue IntDivisionByZeroError() {
return ErrorValue(absl::InvalidArgumentError("int division by zero"));
}

ErrorValue UintDivisionByZeroError() {
return ErrorValue(absl::InvalidArgumentError("uint division by zero"));
}

ErrorValue IntModuloByZeroError() {
return ErrorValue(absl::InvalidArgumentError("int modulo by zero"));
}

ErrorValue UintModuloByZeroError() {
return ErrorValue(absl::InvalidArgumentError("uint modulo by zero"));
}

ErrorValue IntOverflowError() {
return ErrorValue(absl::OutOfRangeError("int overflow"));
}

ErrorValue UintOverflowError() {
return ErrorValue(absl::OutOfRangeError("uint overflow"));
}

ErrorValue DurationOverflowError() {
return ErrorValue(absl::OutOfRangeError("duration overflow"));
}

ErrorValue TimestampOverflowError() {
return ErrorValue(absl::OutOfRangeError("timestamp overflow"));
}

ErrorValue DoubleToIntOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("double out of int range"));
}

ErrorValue DoubleToUintOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("double out of uint range"));
}

ErrorValue IntToUintOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("int out of uint range"));
}

ErrorValue UintToIntOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("uint out of int range"));
}

ErrorValue Int64ToInt32OutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("int64 out of int32_t range"));
}

ErrorValue Uint64ToUint32OutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("uint64 out of uint32_t range"));
}

bool IsNoSuchField(const ErrorValue& value) {
return absl::IsNotFound(value.NativeValue()) &&
absl::StartsWith(value.NativeValue().message(), "no_such_field");
Expand Down
28 changes: 28 additions & 0 deletions common/values/error_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ ErrorValue IndexOutOfBoundsError(size_t index);

ErrorValue IndexOutOfBoundsError(ptrdiff_t index);

ErrorValue IntDivisionByZeroError();

ErrorValue UintDivisionByZeroError();

ErrorValue IntModuloByZeroError();

ErrorValue UintModuloByZeroError();

ErrorValue IntOverflowError();

ErrorValue UintOverflowError();

ErrorValue DurationOverflowError();

ErrorValue TimestampOverflowError();

ErrorValue DoubleToIntOutOfRangeError();

ErrorValue DoubleToUintOutOfRangeError();

ErrorValue IntToUintOutOfRangeError();

ErrorValue UintToIntOutOfRangeError();

ErrorValue Int64ToInt32OutOfRangeError();

ErrorValue Uint64ToUint32OutOfRangeError();

// Catch other integrals and forward them to the above ones. This is needed to
// avoid ambiguous overload issues for smaller integral types like `int`.
template <typename T>
Expand Down
6 changes: 3 additions & 3 deletions eval/eval/function_step_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,9 @@ TEST_F(DirectFunctionStepTest, ErrorHandlingCall) {
Activation activation;
ASSERT_OK_AND_ASSIGN(auto value, plan->Evaluate(activation, &arena_));

EXPECT_THAT(value,
test::IsCelError(StatusIs(absl::StatusCode::kInvalidArgument,
testing::HasSubstr("divide by zero"))));
EXPECT_THAT(value, test::IsCelError(
StatusIs(absl::StatusCode::kInvalidArgument,
testing::HasSubstr("division by zero"))));
}

TEST_F(DirectFunctionStepTest, NoOverload) {
Expand Down
1 change: 1 addition & 0 deletions eval/public/structs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_protobuf//:protobuf",
],
)
Expand Down
4 changes: 2 additions & 2 deletions eval/public/structs/cel_proto_wrap_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ google::protobuf::Message* Int32FromValue(const google::protobuf::Message* proto
if (!value.GetValue(&val)) {
return nullptr;
}
if (!cel::internal::CheckedInt64ToInt32(val).ok()) {
if (!cel::internal::CheckedInt64ToInt32(val)) {
return nullptr;
}
int32_t ival = static_cast<int32_t>(val);
Expand Down Expand Up @@ -882,7 +882,7 @@ google::protobuf::Message* UInt32FromValue(const google::protobuf::Message* prot
if (!value.GetValue(&val)) {
return nullptr;
}
if (!cel::internal::CheckedUint64ToUint32(val).ok()) {
if (!cel::internal::CheckedUint64ToUint32(val)) {
return nullptr;
}
uint32_t ival = static_cast<uint32_t>(val);
Expand Down
9 changes: 5 additions & 4 deletions eval/public/structs/field_access_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "absl/types/optional.h"
#include "eval/public/structs/cel_proto_wrap_util.h"
#include "internal/casts.h"
#include "internal/overflow.h"
Expand Down Expand Up @@ -357,9 +358,9 @@ class FieldSetter {
if (!cel_value.GetValue(&value)) {
return false;
}
absl::StatusOr<int32_t> checked_cast =
absl::optional<int32_t> checked_cast =
cel::internal::CheckedInt64ToInt32(value);
if (!checked_cast.ok()) {
if (!checked_cast) {
return false;
}
static_cast<const Derived*>(this)->SetInt32(*checked_cast);
Expand All @@ -371,7 +372,7 @@ class FieldSetter {
if (!cel_value.GetValue(&value)) {
return false;
}
if (!cel::internal::CheckedUint64ToUint32(value).ok()) {
if (!cel::internal::CheckedUint64ToUint32(value)) {
return false;
}
static_cast<const Derived*>(this)->SetUInt32(value);
Expand Down Expand Up @@ -437,7 +438,7 @@ class FieldSetter {
if (!cel_value.GetValue(&value)) {
return false;
}
if (!cel::internal::CheckedInt64ToInt32(value).ok()) {
if (!cel::internal::CheckedInt64ToInt32(value)) {
return false;
}
static_cast<const Derived*>(this)->SetEnum(value);
Expand Down
9 changes: 4 additions & 5 deletions internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ cc_library(

cc_library(
name = "overflow",
srcs = ["overflow.cc"],
hdrs = ["overflow.h"],
deps = [
":status_macros",
":time",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/base:config",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:optional",
],
)

Expand All @@ -101,9 +99,10 @@ cc_test(
deps = [
":overflow",
":testing",
":time",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/status",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:optional",
],
)

Expand Down
Loading