From 2345c54796b91e0c6d6f1a50c009b0cf8006f43d Mon Sep 17 00:00:00 2001 From: ci-bot Date: Mon, 1 Sep 2025 17:23:08 -0700 Subject: [PATCH 1/3] Core: Prevent AIOOBE for negative policy codes in PredefinedPolicyTypes.fromCode; add unit test --- .../core/policy/PredefinedPolicyTypes.java | 2 +- .../policy/PredefinedPolicyTypesTest.java | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java diff --git a/polaris-core/src/main/java/org/apache/polaris/core/policy/PredefinedPolicyTypes.java b/polaris-core/src/main/java/org/apache/polaris/core/policy/PredefinedPolicyTypes.java index 6cb86eb52f..40128f979b 100644 --- a/polaris-core/src/main/java/org/apache/polaris/core/policy/PredefinedPolicyTypes.java +++ b/polaris-core/src/main/java/org/apache/polaris/core/policy/PredefinedPolicyTypes.java @@ -88,7 +88,7 @@ public boolean isInheritable() { */ @JsonCreator public static @Nullable PredefinedPolicyTypes fromCode(int code) { - if (code >= REVERSE_CODE_MAPPING_ARRAY.length) { + if (code < 0 || code >= REVERSE_CODE_MAPPING_ARRAY.length) { return null; } diff --git a/polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java b/polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java new file mode 100644 index 0000000000..97bfc2417b --- /dev/null +++ b/polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.polaris.core.policy; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class PredefinedPolicyTypesTest { + + @Test + void fromCodeReturnsNullForNegative() { + assertThat(PredefinedPolicyTypes.fromCode(-1)).isNull(); + } + + @Test + void fromCodeReturnsTypeForValid() { + int code = PredefinedPolicyTypes.DATA_COMPACTION.getCode(); + assertThat(PredefinedPolicyTypes.fromCode(code)) + .isEqualTo(PredefinedPolicyTypes.DATA_COMPACTION); + } + + @Test + void fromNameReturnsNullForUnknown() { + assertThat(PredefinedPolicyTypes.fromName("__unknown__")).isNull(); + } +} + From 65ac17a76efa84c1d05b16434fe9b61e310bf7a2 Mon Sep 17 00:00:00 2001 From: Yufei Gu Date: Wed, 3 Sep 2025 10:38:42 -0700 Subject: [PATCH 2/3] Resolve comments --- .../polaris/core/policy/PolicyTypeTest.java | 13 ++++++ .../policy/PredefinedPolicyTypesTest.java | 44 ------------------- 2 files changed, 13 insertions(+), 44 deletions(-) delete mode 100644 polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java diff --git a/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java b/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java index 40cc2d6164..ebbc54183c 100644 --- a/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java +++ b/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java @@ -20,10 +20,13 @@ import java.util.stream.Stream; import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import static org.assertj.core.api.Assertions.assertThat; + public class PolicyTypeTest { static Stream predefinedPolicyTypes() { @@ -53,4 +56,14 @@ public void testPredefinedPolicyTypeFromName(int code, String name, boolean isIn Assertions.assertThat(policyType.getName()).isEqualTo(name); Assertions.assertThat(policyType.isInheritable()).isEqualTo(isInheritable); } + + @Test + void fromCodeReturnsNullForNegative() { + assertThat(PolicyType.fromCode(-1)).isNull(); + } + + @Test + void fromNameReturnsNullForUnknown() { + assertThat(PolicyType.fromName("__unknown__")).isNull(); + } } diff --git a/polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java b/polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java deleted file mode 100644 index 97bfc2417b..0000000000 --- a/polaris-core/src/test/java/org/apache/polaris/core/policy/PredefinedPolicyTypesTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.polaris.core.policy; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -class PredefinedPolicyTypesTest { - - @Test - void fromCodeReturnsNullForNegative() { - assertThat(PredefinedPolicyTypes.fromCode(-1)).isNull(); - } - - @Test - void fromCodeReturnsTypeForValid() { - int code = PredefinedPolicyTypes.DATA_COMPACTION.getCode(); - assertThat(PredefinedPolicyTypes.fromCode(code)) - .isEqualTo(PredefinedPolicyTypes.DATA_COMPACTION); - } - - @Test - void fromNameReturnsNullForUnknown() { - assertThat(PredefinedPolicyTypes.fromName("__unknown__")).isNull(); - } -} - From 55808c20d540016e97cda5e94cbda2b16bb2f6c0 Mon Sep 17 00:00:00 2001 From: Yufei Gu Date: Wed, 3 Sep 2025 11:29:56 -0700 Subject: [PATCH 3/3] fix style issue --- .../java/org/apache/polaris/core/policy/PolicyTypeTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java b/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java index ebbc54183c..d120d32482 100644 --- a/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java +++ b/polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyTypeTest.java @@ -18,6 +18,8 @@ */ package org.apache.polaris.core.policy; +import static org.assertj.core.api.Assertions.assertThat; + import java.util.stream.Stream; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,8 +27,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import static org.assertj.core.api.Assertions.assertThat; - public class PolicyTypeTest { static Stream predefinedPolicyTypes() {