From 794e197d464bc537b71026874a55f9a94b13066f Mon Sep 17 00:00:00 2001 From: Justin King Date: Mon, 30 Nov 2026 11:25:06 -0800 Subject: [PATCH] Fix behavior of out-of-bounds list indexing when using optional PiperOrigin-RevId: 1032826289 --- eval/eval/container_access_step.cc | 23 ++++++++++++++++++----- runtime/optional_types_test.cc | 3 +++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/eval/eval/container_access_step.cc b/eval/eval/container_access_step.cc index 5e62cb7c6..6b7ef3cca 100644 --- a/eval/eval/container_access_step.cc +++ b/eval/eval/container_access_step.cc @@ -1,5 +1,6 @@ #include "eval/eval/container_access_step.h" +#include #include #include #include @@ -8,6 +9,7 @@ #include "absl/log/absl_check.h" #include "absl/status/status.h" #include "absl/status/statusor.h" +#include "absl/strings/match.h" #include "absl/strings/str_cat.h" #include "absl/types/optional.h" #include "absl/types/span.h" @@ -159,6 +161,17 @@ void LookupInMap(const MapValue& cel_map, const Value& key, ABSL_DCHECK(!result.IsUnknown()); } +absl::Status CreateNoSuchIndexError(int64_t index, size_t size) { + return absl::UnknownError( + absl::StrCat("Index error: index=", index, " size=", size)); +} + +[[nodiscard]] +bool IsNoSuchIndexError(const absl::Status& status) { + return status.code() == absl::StatusCode::kUnknown && + absl::StartsWith(status.message(), "Index error: index="); +} + void LookupInList(const ListValue& cel_list, const Value& key, ExecutionFrameBase& frame, Value& result) { absl::optional maybe_idx; @@ -187,10 +200,8 @@ void LookupInList(const ListValue& cel_list, const Value& key, return; } if (idx < 0 || idx >= *size) { - result = - cel::ErrorValue::From(absl::UnknownError(absl::StrCat( - "Index error: index=", idx, " size=", *size)), - frame.arena()); + result = cel::ErrorValue::From(CreateNoSuchIndexError(idx, *size), + frame.arena()); return; } @@ -270,7 +281,9 @@ void PerformLookup(ExecutionFrameBase& frame, const Value& container, optional_value.Value(&value); LookupInContainer(value, key, frame, result); if (auto error_value = result.AsError(); - error_value && cel::IsNoSuchKey(*error_value)) { + error_value.has_value() && + (cel::IsNoSuchKey(*error_value) || + IsNoSuchIndexError(error_value->ToStatus()))) { result = cel::OptionalValue::None(); return; } diff --git a/runtime/optional_types_test.cc b/runtime/optional_types_test.cc index 578b6d5bc..aa5c48751 100644 --- a/runtime/optional_types_test.cc +++ b/runtime/optional_types_test.cc @@ -54,6 +54,7 @@ using ::cel::test::BoolValueIs; using ::cel::test::IntValueIs; using ::cel::test::OptionalValueIs; using ::cel::test::OptionalValueIsEmpty; +using ::cel::test::StringValueIs; using ::cel::expr::ParsedExpr; using ::google::api::expr::parser::Parse; using ::google::api::expr::parser::ParserOptions; @@ -264,6 +265,8 @@ INSTANTIATE_TEST_SUITE_P( {"list_first_empty", "[].first()", OptionalValueIsEmpty()}, {"list_last", "[1, 2, 3].last()", OptionalValueIs(IntValueIs(3))}, {"list_last_empty", "[].last()", OptionalValueIsEmpty()}, + {"list_empty_index_orValue", "optional.of([])[0].orValue(\"test\")", + StringValueIs("test")}, }), /*enable_short_circuiting*/ testing::Bool()));