From 4d5f0e7ad5d20a754a484d0ce9ae2d771c0f8c1e Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 18 Sep 2017 21:32:31 -0700 Subject: [PATCH] NativeLibraryLoader should check the result of ClassLoader#getResource method Motivation: NativeLibraryLoader uses ClassLoader#getResource method that can return nulls when the resource cannot be found. The returned url variable should be checked for nullity and fail in a more usable manner than a NullPointerException Modifications: Fail with a FileNotFoundException Result: Fixes [#7222]. --- .../util/internal/NativeLibraryLoader.java | 31 ++++++++++------ .../internal/NativeLibraryLoaderTest.java | 37 +++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 common/src/test/java/io/netty/util/internal/NativeLibraryLoaderTest.java diff --git a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java index dc3e359a78..a1ea75fc2f 100644 --- a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java +++ b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java @@ -21,6 +21,7 @@ import io.netty.util.internal.logging.InternalLoggerFactory; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -125,22 +126,28 @@ public final class NativeLibraryLoader { String libname = System.mapLibraryName(name); String path = NATIVE_RESOURCE_HOME + libname; - URL url = loader.getResource(path); - if (url == null && PlatformDependent.isOsx()) { - if (path.endsWith(".jnilib")) { - url = loader.getResource(NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib"); - } else { - url = loader.getResource(NATIVE_RESOURCE_HOME + "lib" + name + ".jnilib"); - } - } - - int index = libname.lastIndexOf('.'); - String prefix = libname.substring(0, index); - String suffix = libname.substring(index, libname.length()); InputStream in = null; OutputStream out = null; File tmpFile = null; + URL url = loader.getResource(path); try { + if (url == null) { + if (PlatformDependent.isOsx()) { + String fileName = path.endsWith(".jnilib") ? NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib" : + NATIVE_RESOURCE_HOME + "lib" + name + ".jnilib"; + url = loader.getResource(fileName); + if (url == null) { + throw new FileNotFoundException(fileName); + } + } else { + throw new FileNotFoundException(path); + } + } + + int index = libname.lastIndexOf('.'); + String prefix = libname.substring(0, index); + String suffix = libname.substring(index, libname.length()); + tmpFile = File.createTempFile(prefix, suffix, WORKDIR); in = url.openStream(); out = new FileOutputStream(tmpFile); diff --git a/common/src/test/java/io/netty/util/internal/NativeLibraryLoaderTest.java b/common/src/test/java/io/netty/util/internal/NativeLibraryLoaderTest.java new file mode 100644 index 0000000000..151e3fc5bc --- /dev/null +++ b/common/src/test/java/io/netty/util/internal/NativeLibraryLoaderTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2017 The Netty Project + * + * The Netty Project 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 io.netty.util.internal; + +import org.junit.Test; + +import java.io.FileNotFoundException; +import java.util.UUID; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class NativeLibraryLoaderTest { + + @Test + public void testFileNotFound() { + try { + NativeLibraryLoader.load(UUID.randomUUID().toString(), NativeLibraryLoaderTest.class.getClassLoader()); + fail(); + } catch (UnsatisfiedLinkError error) { + assertTrue(error.getCause() instanceof FileNotFoundException); + } + } +}