Skip to content

Commit 983114c

Browse files
l46kokcopybara-github
authored andcommitted
Prevent planning createMap for heterogeneous duplicate keys
PiperOrigin-RevId: 875399236
1 parent 3cebc1a commit 983114c

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

conformance/src/test/java/dev/cel/conformance/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ _TESTS_TO_SKIP_PLANNER = [
175175
"timestamps/timestamp_range/sub_time_duration_under",
176176

177177
# Skip until fixed.
178-
"fields/qualified_identifier_resolution/map_value_repeat_key_heterogeneous",
179178
"optionals/optionals/map_null_entry_no_such_key",
180179
"optionals/optionals/map_present_key_invalid_field",
181180
"parse/receiver_function_names",

runtime/src/main/java/dev/cel/runtime/planner/EvalCreateMap.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,26 @@ public Object eval(GlobalResolver resolver, ExecutionFrame frame) throws CelEval
5959
keyInterpretable.exprId());
6060
}
6161

62-
Object val = values[i].eval(resolver, frame);
62+
boolean isDuplicate = !keysSeen.add(key);
63+
if (!isDuplicate) {
64+
if (key instanceof Long) {
65+
long longVal = (Long) key;
66+
if (longVal >= 0) {
67+
isDuplicate = keysSeen.contains(UnsignedLong.valueOf(longVal));
68+
}
69+
} else if (key instanceof UnsignedLong) {
70+
UnsignedLong ulongVal = (UnsignedLong) key;
71+
isDuplicate = keysSeen.contains(ulongVal.longValue());
72+
}
73+
}
6374

64-
if (!keysSeen.add(key)) {
75+
if (isDuplicate) {
6576
throw new LocalizedEvaluationException(
6677
CelDuplicateKeyException.of(key), keyInterpretable.exprId());
6778
}
6879

80+
Object val = values[i].eval(resolver, frame);
81+
6982
if (isOptional[i]) {
7083
if (!(val instanceof Optional)) {
7184
throw new IllegalArgumentException(

0 commit comments

Comments
 (0)