diff --git a/eval/public/extension_func_registrar.cc b/eval/public/extension_func_registrar.cc index d3411e9fc..1046eb205 100644 --- a/eval/public/extension_func_registrar.cc +++ b/eval/public/extension_func_registrar.cc @@ -109,14 +109,12 @@ CelValue GetTimeOfDayUTC(Arena* arena, absl::Time time_stamp) { return GetTimeOfDayTz(arena, time_stamp, utc); } -int ToSeconds(const google::type::TimeOfDay* time_of_day) { - int seconds = 0; - - seconds += time_of_day->hours() * 60 * 60; - seconds += time_of_day->minutes() * 60; - seconds += time_of_day->seconds(); - - return seconds; +// Fields are widened to int64_t so that out-of-range TimeOfDay values cannot +// overflow (and wrap into the valid range) in 32-bit arithmetic. +int64_t ToSeconds(const google::type::TimeOfDay* time_of_day) { + return static_cast(time_of_day->hours()) * 60 * 60 + + static_cast(time_of_day->minutes()) * 60 + + static_cast(time_of_day->seconds()); } CelValue BetweenToD(Arena* arena, const google::protobuf::Message* time_of_day, @@ -135,9 +133,9 @@ CelValue BetweenToD(Arena* arena, const google::protobuf::Message* time_of_day, absl::StatusCode::kInvalidArgument); } // resolution for TimeOfDay in this function is 1 second - int start_time = ToSeconds(start_tod); - int stop_time = ToSeconds(stop_tod); - int tod_time = ToSeconds(time_of_day_tod); + int64_t start_time = ToSeconds(start_tod); + int64_t stop_time = ToSeconds(stop_tod); + int64_t tod_time = ToSeconds(time_of_day_tod); is_between = (tod_time >= start_time) && (tod_time < stop_time); return CelValue::CreateBool(is_between); diff --git a/eval/public/extension_func_test.cc b/eval/public/extension_func_test.cc index c9f5fb4fa..b10ac9ed1 100644 --- a/eval/public/extension_func_test.cc +++ b/eval/public/extension_func_test.cc @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -544,6 +546,41 @@ TEST_F(ExtensionTest, TestBetweenToD) { ASSERT_EQ(result.BoolOrDie(), false); } +TEST_F(ExtensionTest, TestBetweenToDOutOfRangeHoursDoNotWrap) { + Arena arena; + CelValue result; + google::type::TimeOfDay* time_of_day = + Arena::Create(&arena); + google::type::TimeOfDay* start = + Arena::Create(&arena); + google::type::TimeOfDay* stop = + Arena::Create(&arena); + + start->set_hours(0); + stop->set_hours(1); + // 1193047 hours is 4294969200 seconds, which wraps to 1904 seconds (00:31:44) + // in 32-bit arithmetic and would wrongly land inside [00:00, 01:00). + time_of_day->set_hours(1193047); + + PerformBetweenToDTest(&arena, time_of_day, start, stop, &result); + ASSERT_TRUE(result.IsBool()); + EXPECT_FALSE(result.BoolOrDie()); + + time_of_day->set_hours(std::numeric_limits::max()); + time_of_day->set_minutes(std::numeric_limits::max()); + time_of_day->set_seconds(std::numeric_limits::max()); + PerformBetweenToDTest(&arena, time_of_day, start, stop, &result); + ASSERT_TRUE(result.IsBool()); + EXPECT_FALSE(result.BoolOrDie()); + + time_of_day->set_hours(std::numeric_limits::min()); + time_of_day->set_minutes(std::numeric_limits::min()); + time_of_day->set_seconds(std::numeric_limits::min()); + PerformBetweenToDTest(&arena, time_of_day, start, stop, &result); + ASSERT_TRUE(result.IsBool()); + EXPECT_FALSE(result.BoolOrDie()); +} + TEST_F(ExtensionTest, TestBetweenTodStr) { Arena arena; CelValue result; diff --git a/runtime/standard/BUILD b/runtime/standard/BUILD index d08ac2116..0f372c412 100644 --- a/runtime/standard/BUILD +++ b/runtime/standard/BUILD @@ -95,10 +95,18 @@ cc_test( "//base:builtins", "//common:function_descriptor", "//common:kind", + "//common:value", + "//common:value_testing", "//internal:testing", + "//internal:testing_descriptor_pool", + "//internal:testing_message_factory", + "//runtime:function", + "//runtime:function_overload_reference", "//runtime:function_registry", "//runtime:runtime_options", + "@com_google_absl//absl/status:status_matchers", "@com_google_absl//absl/strings", + "@com_google_protobuf//:protobuf", ], ) diff --git a/runtime/standard/container_membership_functions.cc b/runtime/standard/container_membership_functions.cc index 26c115103..cfa8a78d4 100644 --- a/runtime/standard/container_membership_functions.cc +++ b/runtime/standard/container_membership_functions.cc @@ -250,8 +250,8 @@ absl::Status RegisterMapMembershipFunctions(FunctionRegistry& registry, CEL_RETURN_IF_ERROR(map_value.Has(IntValue(number.AsInt()), descriptor_pool, message_factory, arena, &has_alt)); - if (has.IsTrue()) { - return has; + if (has_alt.IsTrue()) { + return has_alt; } } return BoolValue(false); diff --git a/runtime/standard/container_membership_functions_test.cc b/runtime/standard/container_membership_functions_test.cc index 9c90136d9..e7145bbf1 100644 --- a/runtime/standard/container_membership_functions_test.cc +++ b/runtime/standard/container_membership_functions_test.cc @@ -15,19 +15,33 @@ #include "runtime/standard/container_membership_functions.h" #include +#include +#include +#include #include +#include "absl/status/status_matchers.h" #include "absl/strings/string_view.h" #include "base/builtins.h" #include "common/function_descriptor.h" #include "common/kind.h" +#include "common/value.h" +#include "common/value_testing.h" #include "internal/testing.h" +#include "internal/testing_descriptor_pool.h" +#include "internal/testing_message_factory.h" +#include "runtime/function.h" +#include "runtime/function_overload_reference.h" #include "runtime/function_registry.h" #include "runtime/runtime_options.h" +#include "google/protobuf/arena.h" namespace cel { namespace { +using ::absl_testing::IsOk; +using ::cel::test::BoolValueIs; +using ::testing::SizeIs; using ::testing::UnorderedElementsAre; MATCHER_P3(MatchesDescriptor, name, receiver, expected_kinds, "") { @@ -131,6 +145,48 @@ TEST(RegisterContainerMembershipFunctions, RegistersInOperatorListsDisabled) { } } +// When enable_fast_builtins is true, the planner routes heterogeneous `in` +// through a dedicated step; invoke the registered overload directly to cover +// the uint -> int key fallback used when enable_fast_builtins is false. +TEST(RegisterContainerMembershipFunctions, HeterogeneousUintKeyMatchesIntKey) { + FunctionRegistry registry; + RuntimeOptions options; + options.enable_heterogeneous_equality = true; + ASSERT_THAT(RegisterContainerMembershipFunctions(registry, options), IsOk()); + + google::protobuf::Arena arena; + auto builder = NewMapValueBuilder(&arena); + ASSERT_THAT(builder->Put(IntValue(1), IntValue(2)), IsOk()); + ASSERT_THAT(builder->Put(IntValue(-1), IntValue(3)), IsOk()); + MapValue map_value = std::move(*builder).Build(); + + for (absl::string_view operator_name : kInOperators) { + std::vector overloads = + registry.FindStaticOverloads(operator_name, /*receiver_style=*/false, + {Kind::kUint, Kind::kMap}); + ASSERT_THAT(overloads, SizeIs(1)); + const Function::InvokeContext context(internal::GetTestingDescriptorPool(), + internal::GetTestingMessageFactory(), + &arena); + + ASSERT_OK_AND_ASSIGN(Value found, overloads[0].implementation.Invoke( + {UintValue(1), map_value}, context)); + EXPECT_THAT(found, BoolValueIs(true)); + + ASSERT_OK_AND_ASSIGN( + Value missing, + overloads[0].implementation.Invoke({UintValue(2), map_value}, context)); + EXPECT_THAT(missing, BoolValueIs(false)); + + ASSERT_OK_AND_ASSIGN( + Value unconvertible, + overloads[0].implementation.Invoke( + {UintValue(std::numeric_limits::max()), map_value}, + context)); + EXPECT_THAT(unconvertible, BoolValueIs(false)); + } +} + // TODO(uncreated-issue/41): move functional parsed expr tests when modern APIs for // evaluator available. diff --git a/runtime/standard_runtime_builder_factory_test.cc b/runtime/standard_runtime_builder_factory_test.cc index 07bc7f1f9..789d43644 100644 --- a/runtime/standard_runtime_builder_factory_test.cc +++ b/runtime/standard_runtime_builder_factory_test.cc @@ -443,6 +443,33 @@ INSTANTIATE_TEST_SUITE_P( {"list_in", "'a' in ['a', 'b', 'c', 'd']", true}, {"list_in_numeric", "3u in [1.1, 2.3, 3.0, 4.4]", true}})); +TEST(StandardRuntimeTest, HeterogeneousInMapWithoutFastBuiltins) { + // Without fast builtins the planner dispatches `in` to the registered map + // overloads, so heterogeneous numeric key fallbacks must be honored there. + RuntimeOptions options; + options.enable_fast_builtins = false; + + ASSERT_OK_AND_ASSIGN(auto builder, + CreateStandardRuntimeBuilder( + google::protobuf::DescriptorPool::generated_pool(), options)); + ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build()); + + for (const auto& test_case : std::vector>{ + {"1u in {1: 1, 2: 2}", true}, + {"3u in {1: 1, 2: 2}", false}, + {"1 in {1u: 1, 2u: 2}", true}, + {"1.0 in {1: 1, 2: 2}", true}, + }) { + ASSERT_OK_AND_ASSIGN(ParsedExpr expr, ParseWithTestMacros(test_case.first)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr program, + ProtobufRuntimeAdapter::CreateProgram(*runtime, expr)); + google::protobuf::Arena arena; + Activation activation; + ASSERT_OK_AND_ASSIGN(Value result, program->Evaluate(&arena, activation)); + EXPECT_THAT(result, BoolValueIs(test_case.second)) << test_case.first; + } +} + TEST(StandardRuntimeTest, RuntimeIssueSupport) { RuntimeOptions options; options.fail_on_warnings = false;