From 7f69e092900dde1dcb1def8f9402627dd082c1c2 Mon Sep 17 00:00:00 2001 From: FuzzingTeam <114765570+FuzzingTeam@users.noreply.github.com> Date: Thu, 20 Oct 2022 22:42:17 +0530 Subject: [PATCH] HADOOP-18471. Fixed ArrayIndexOutOfBoundsException in DefaultStringifier (#4957) Contributed by FuzzingTeam --- .../main/java/org/apache/hadoop/io/DefaultStringifier.java | 3 +++ .../java/org/apache/hadoop/io/TestDefaultStringifier.java | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DefaultStringifier.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DefaultStringifier.java index 7453996ecab..7be50b0c539 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DefaultStringifier.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DefaultStringifier.java @@ -158,6 +158,9 @@ public class DefaultStringifier implements Stringifier { public static void storeArray(Configuration conf, K[] items, String keyName) throws IOException { + if (items.length == 0) { + throw new IndexOutOfBoundsException(); + } DefaultStringifier stringifier = new DefaultStringifier(conf, GenericsUtil.getClass(items[0])); try { diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestDefaultStringifier.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestDefaultStringifier.java index b70e011f6aa..c15ec8caa4f 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestDefaultStringifier.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestDefaultStringifier.java @@ -26,6 +26,7 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.hadoop.test.LambdaTestUtils.intercept; import static org.junit.Assert.assertEquals; public class TestDefaultStringifier { @@ -98,7 +99,7 @@ public class TestDefaultStringifier { } @Test - public void testStoreLoadArray() throws IOException { + public void testStoreLoadArray() throws Exception { LOG.info("Testing DefaultStringifier#storeArray() and #loadArray()"); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization"); @@ -107,6 +108,8 @@ public class TestDefaultStringifier { Integer[] array = new Integer[] {1,2,3,4,5}; + intercept(IndexOutOfBoundsException.class, () -> + DefaultStringifier.storeArray(conf, new Integer[] {}, keyName)); DefaultStringifier.storeArray(conf, array, keyName); Integer[] claimedArray = DefaultStringifier.loadArray(conf, keyName, Integer.class);