Skip to content

Commit

Permalink
Add a null-check in Pack200.newInstance(String, String)
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 12, 2023
1 parent 956e15b commit a770a37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" issue="COMPRESS-632" dev="ggregory" due-to="Yakov Shafranovich, Gary Gregory">Improve CPIO exception detection and handling #441.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate SkipShieldingInputStream without replacement (no longer used).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Reuse commons-codec, don't duplicate package-private class PureJavaCrc32C.</action>
<action type="fix" dev="ggregory" due-to="alumi, Gary Gregory">Add a null check for the class loader of OsgiUtils #451.</action>
<action type="fix" dev="ggregory" due-to="alumi, Gary Gregory">Add a null-check for the class loader of OsgiUtils #451.</action>
<action type="fix" dev="ggregory" due-to="alumi, Gary Gregory">Add a null-check in Pack200.newInstance(String, String).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-lang3 from 3.13.0 to 3.14.0.</action>
<action type="update" dev="ggregory" due-to="Dependabot">Bump com.github.marschall:memoryfilesystem from 2.6.1 to 2.7.0 #444.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.OutputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Objects;
import java.util.SortedMap;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
Expand All @@ -41,22 +42,22 @@ public abstract class Pack200 {
public interface Packer {

/**
* the format of a class attribute name.
* The format of a class attribute name.
*/
String CLASS_ATTRIBUTE_PFX = "pack.class.attribute."; //$NON-NLS-1$

/**
* the format of a code attribute name.
* The format of a code attribute name.
*/
String CODE_ATTRIBUTE_PFX = "pack.code.attribute."; //$NON-NLS-1$

/**
* the deflation hint to set in the output archive.
* The deflation hint to set in the output archive.
*/
String DEFLATE_HINT = "pack.deflate.hint";//$NON-NLS-1$

/**
* the indicated amount of effort to use in compressing the archive.
* The indicated amount of effort to use in compressing the archive.
*/
String EFFORT = "pack.effort";//$NON-NLS-1$

Expand All @@ -71,47 +72,47 @@ public interface Packer {
String FALSE = "false";//$NON-NLS-1$

/**
* the format of a field attribute name.
* The format of a field attribute name.
*/
String FIELD_ATTRIBUTE_PFX = "pack.field.attribute.";//$NON-NLS-1$

/**
* a String representation for {@code keep}.
* The String representation for {@code keep}.
*/
String KEEP = "keep";//$NON-NLS-1$

/**
* decide if all elements shall transmit in their original order.
* Decide if all elements shall transmit in their original order.
*/
String KEEP_FILE_ORDER = "pack.keep.file.order";//$NON-NLS-1$

/**
* a String representation for {@code latest}.
* The String representation for {@code latest}.
*/
String LATEST = "latest";//$NON-NLS-1$

/**
* the format of a method attribute name.
* The format of a method attribute name.
*/
String METHOD_ATTRIBUTE_PFX = "pack.method.attribute.";//$NON-NLS-1$

/**
* if it shall attempt to determine the latest modification time if this is set to {@code LATEST}.
* If it shall attempt to determine the latest modification time if this is set to {@code LATEST}.
*/
String MODIFICATION_TIME = "pack.modification.time";//$NON-NLS-1$

/**
* a String representation of {@code pass}.
* The String representation of {@code pass}.
*/
String PASS = "pass";//$NON-NLS-1$

/**
* the file that will not be compressed.
* The file that will not be compressed.
*/
String PASS_FILE_PFX = "pack.pass.file.";//$NON-NLS-1$

/**
* packer progress as a percentage.
* Packer progress as a percentage.
*/
String PROGRESS = "pack.progress";//$NON-NLS-1$

Expand All @@ -121,22 +122,22 @@ public interface Packer {
String SEGMENT_LIMIT = "pack.segment.limit";//$NON-NLS-1$

/**
* a String representation of {@code strip}.
* The String representation of {@code strip}.
*/
String STRIP = "strip";//$NON-NLS-1$

/**
* a String representation of {@code true}.
* The String representation of {@code true}.
*/
String TRUE = "true";//$NON-NLS-1$

/**
* the action to take if an unknown attribute is encountered.
* The action to take if an unknown attribute is encountered.
*/
String UNKNOWN_ATTRIBUTE = "pack.unknown.attribute";//$NON-NLS-1$

/**
* add a listener for PropertyChange events
* Add a listener for PropertyChange events
*
* @param listener the listener to listen if PropertyChange events occurs
*/
Expand Down Expand Up @@ -196,7 +197,7 @@ public interface Unpacker {
String KEEP = "keep";//$NON-NLS-1$

/**
* the progress as a {@code percentage}.
* The progress as a {@code percentage}.
*/
String PROGRESS = "unpack.progress";//$NON-NLS-1$

Expand Down Expand Up @@ -245,16 +246,26 @@ public interface Unpacker {
void unpack(InputStream in, JarOutputStream out) throws IOException;
}

/**
* System property key.
*/
private static final String SYSTEM_PROPERTY_PACKER = "java.util.jar.Pack200.Packer"; //$NON-NLS-1$

/**
* System property key.
*/
private static final String SYSTEM_PROPERTY_UNPACKER = "java.util.jar.Pack200.Unpacker"; //$NON-NLS-1$

static Object newInstance(final String systemProperty, final String defaultClassName) {
return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
final String className = System.getProperty(systemProperty, defaultClassName);
try {
// TODO Not sure if this will cause problems loading the class
return Pack200.class.getClassLoader().loadClass(className).getConstructor().newInstance();
ClassLoader classLoader = Pack200.class.getClassLoader();
if (classLoader == null) {
classLoader = Objects.requireNonNull(ClassLoader.getSystemClassLoader(), "ClassLoader.getSystemClassLoader()");
}
return classLoader.loadClass(className).getConstructor().newInstance();
} catch (final Exception e) {
throw new Error(Messages.getString("archive.3E", className), e); //$NON-NLS-1$
}
Expand All @@ -266,6 +277,7 @@ static Object newInstance(final String systemProperty, final String defaultClass
* <p>
* The implementation of the packer engine is defined by the system property {@code 'java.util.jar.Pack200.Packer'}. If this system property is defined an
* instance of the specified class is returned, otherwise the system's default implementation is returned.
* </p>
*
* @return an instance of {@code Packer}
*/
Expand All @@ -278,6 +290,7 @@ public static Pack200.Packer newPacker() {
* <p>
* The implementation of the unpacker engine is defined by the system property {@link Pack200.Unpacker}. If this system property is defined an instance of
* the specified class is returned, otherwise the system's default implementation is returned.
* </p>
*
* @return an instance of {@link Pack200.Unpacker}.
*/
Expand Down

0 comments on commit a770a37

Please sign in to comment.