diff --git a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c index afa4720e507..56f0f71eb5a 100644 --- a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c +++ b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c @@ -363,6 +363,15 @@ Java_org_apache_hadoop_io_nativeio_NativeIO_00024POSIX_sync_1file_1range( #endif } +#define CHECK_DIRECT_BUFFER_ADDRESS(buf) \ + { \ + if (!buf) { \ + THROW(env, "java/lang/UnsupportedOperationException", \ + "JNI access to direct buffers not available"); \ + return; \ + } \ + } + /** * public static native void mlock_native( * ByteBuffer buffer, long offset); @@ -379,6 +388,7 @@ Java_org_apache_hadoop_io_nativeio_NativeIO_00024POSIX_mlock_1native( PASS_EXCEPTIONS(env); if (mlock(buf, len)) { + CHECK_DIRECT_BUFFER_ADDRESS(buf); throw_ioe(env, errno); } } @@ -399,6 +409,7 @@ Java_org_apache_hadoop_io_nativeio_NativeIO_00024POSIX_munlock_1native( PASS_EXCEPTIONS(env); if (munlock(buf, len)) { + CHECK_DIRECT_BUFFER_ADDRESS(buf); throw_ioe(env, errno); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java index 69c963f2d75..917532e4bf8 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java @@ -545,9 +545,12 @@ public class TestNativeIO { bufSum += buf[i]; } FileOutputStream fos = new FileOutputStream(TEST_FILE); - fos.write(buf); - fos.getChannel().force(true); - fos.close(); + try { + fos.write(buf); + fos.getChannel().force(true); + } finally { + fos.close(); + } FileInputStream fis = null; FileChannel channel = null; diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES-HDFS-4949.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES-HDFS-4949.txt index 1b42824e18d..0505fa7dfcb 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES-HDFS-4949.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES-HDFS-4949.txt @@ -42,3 +42,5 @@ HDFS-4949 (Unreleased) HDFS-5198. NameNodeRpcServer must not send back DNA_FINALIZE in reply to a cache report. (Contributed by Colin Patrick McCabe) + + HDFS-5195. Prevent passing null pointer to mlock and munlock. (cnauroth)