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].
This commit is contained in:
parent
9d56439aa1
commit
4d5f0e7ad5
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user