Skip to content

Commit 2345c54

Browse files
author
ci-bot
committed
Core: Prevent AIOOBE for negative policy codes in PredefinedPolicyTypes.fromCode; add unit test
1 parent d841431 commit 2345c54

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

polaris-core/src/main/java/org/apache/polaris/core/policy/PredefinedPolicyTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public boolean isInheritable() {
8888
*/
8989
@JsonCreator
9090
public static @Nullable PredefinedPolicyTypes fromCode(int code) {
91-
if (code >= REVERSE_CODE_MAPPING_ARRAY.length) {
91+
if (code < 0 || code >= REVERSE_CODE_MAPPING_ARRAY.length) {
9292
return null;
9393
}
9494

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.policy;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
class PredefinedPolicyTypesTest {
26+
27+
@Test
28+
void fromCodeReturnsNullForNegative() {
29+
assertThat(PredefinedPolicyTypes.fromCode(-1)).isNull();
30+
}
31+
32+
@Test
33+
void fromCodeReturnsTypeForValid() {
34+
int code = PredefinedPolicyTypes.DATA_COMPACTION.getCode();
35+
assertThat(PredefinedPolicyTypes.fromCode(code))
36+
.isEqualTo(PredefinedPolicyTypes.DATA_COMPACTION);
37+
}
38+
39+
@Test
40+
void fromNameReturnsNullForUnknown() {
41+
assertThat(PredefinedPolicyTypes.fromName("__unknown__")).isNull();
42+
}
43+
}
44+

0 commit comments

Comments
 (0)