Skip to content

Commit f26fd7d

Browse files
jnthntatumcopybara-github
authored andcommitted
Fix leak for optional.of(string).
PiperOrigin-RevId: 926790246
1 parent ad13761 commit f26fd7d

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

common/values/optional_value.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ OpaqueValue GenericOptionalValueClone(
345345
cel::Value* absl_nonnull result =
346346
::new (arena->AllocateAligned(sizeof(cel::Value), alignof(cel::Value)))
347347
cel::Value(content.To<OptionalValueContent>().value->Clone(arena));
348-
if (!ArenaTraits<>::trivially_destructible(result)) {
348+
if (!ArenaTraits<>::trivially_destructible(*result)) {
349349
arena->OwnDestructor(result);
350350
}
351351
return common_internal::MakeOptionalValue(
@@ -395,7 +395,7 @@ OptionalValue OptionalValue::Of(cel::Value value,
395395
cel::Value* absl_nonnull result = ::new (
396396
arena->AllocateAligned(sizeof(cel::Value), alignof(cel::Value)))
397397
cel::Value(std::move(value));
398-
if (!ArenaTraits<>::trivially_destructible(result)) {
398+
if (!ArenaTraits<>::trivially_destructible(*result)) {
399399
arena->OwnDestructor(result);
400400
}
401401
return OptionalValue(&optional_value_dispatcher,

runtime/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ cc_test(
615615
":activation",
616616
":constant_folding",
617617
":function_adapter",
618+
":optional_types",
618619
":reference_resolver",
619620
":regex_precompilation",
620621
":runtime",

runtime/memory_safety_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "runtime/activation.h"
4646
#include "runtime/constant_folding.h"
4747
#include "runtime/function_adapter.h"
48+
#include "runtime/optional_types.h"
4849
#include "runtime/reference_resolver.h"
4950
#include "runtime/regex_precompilation.h"
5051
#include "runtime/runtime.h"
@@ -174,6 +175,7 @@ absl::StatusOr<std::unique_ptr<Runtime>> ConfigureRuntimeImpl(
174175
if (resolve_references) {
175176
CEL_RETURN_IF_ERROR(EnableReferenceResolver(
176177
runtime_builder, ReferenceResolverEnabled::kAlways));
178+
CEL_RETURN_IF_ERROR(extensions::EnableOptionalTypes(runtime_builder));
177179
}
178180
if (evaluation_options == Options::kFoldConstants) {
179181
CEL_RETURN_IF_ERROR(extensions::EnableConstantFolding(runtime_builder));
@@ -315,6 +317,14 @@ INSTANTIATE_TEST_SUITE_P(
315317
{{"condition", BoolValue(false)}},
316318
test::StringValueIs("long_right_hand_string_0123456789"),
317319
},
320+
{"optional_of_long_const_string",
321+
"condition ? optional.of('lhs_short') : "
322+
"optional.of('long_right_hand_string_0123456789')",
323+
{{"condition", BoolValue(false)}},
324+
test::OptionalValueIs(
325+
test::StringValueIs("long_right_hand_string_0123456789")),
326+
// optional.of is a namespaced function.
327+
/*enable_reference_resolver=*/true},
318328
{
319329
"computed_string",
320330
"(condition ? 'a.b' : 'b.c') + '.d.e.f'",

0 commit comments

Comments
 (0)