From 65b28b91867c44914b85f86ebb05fffa8d404dd9 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 19 Oct 2023 07:21:16 -0400 Subject: [PATCH] Fix possible NPEs in LogFactoryImpl --- src/changes/changes.xml | 3 ++ .../commons/logging/impl/LogFactoryImpl.java | 40 +++++++++++-------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 545f946..cb4d250 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -69,6 +69,9 @@ The type attribute can be add,update,fix,remove. Replace custom code with `ServiceLoader` call. + + Fix possible NPEs in LogFactoryImpl. + Bump Java from 6 to 8. diff --git a/src/main/java/org/apache/commons/logging/impl/LogFactoryImpl.java b/src/main/java/org/apache/commons/logging/impl/LogFactoryImpl.java index cb1ee5f..8bec91b 100644 --- a/src/main/java/org/apache/commons/logging/impl/LogFactoryImpl.java +++ b/src/main/java/org/apache/commons/logging/impl/LogFactoryImpl.java @@ -231,6 +231,14 @@ public class LogFactoryImpl extends LogFactory { return LogFactory.isDiagnosticsEnabled(); } + /** Utility method to safely trim a string. */ + private static String trim(final String src) { + if (src == null) { + return null; + } + return src.trim(); + } + /** * Determines whether logging classes should be loaded using the thread-context * classloader, or via the classloader that loaded this LogFactoryImpl class. @@ -272,14 +280,14 @@ public class LogFactoryImpl extends LogFactory { */ protected Class[] logConstructorSignature = { java.lang.String.class }; + // --------------------------------------------------------- Public Methods + /** * The one-argument {@code setLogFactory} method of the selected * {@link org.apache.commons.logging.Log} method, if it exists. */ protected Method logMethod; - // --------------------------------------------------------- Public Methods - /** * The signature of the {@code setLogFactory} method to be used. */ @@ -374,7 +382,7 @@ public class LogFactoryImpl extends LogFactory { // trying higher up in the hierarchy in this case.. String msg = originalClassNotFoundException.getMessage(); logDiagnostic("The log adapter '" + logAdapterClassName + "' is not available via classloader " + - objectId(currentCL) + ": " + msg.trim()); + objectId(currentCL) + ": " + trim(msg)); try { // Try the class classloader. // This may work in cases where the TCCL @@ -388,7 +396,7 @@ public class LogFactoryImpl extends LogFactory { // no point continuing: this adapter isn't available msg = secondaryClassNotFoundException.getMessage(); logDiagnostic("The log adapter '" + logAdapterClassName + - "' is not available via the LogFactoryImpl class classloader: " + msg.trim()); + "' is not available via the LogFactoryImpl class classloader: " + trim(msg)); break; } } @@ -484,6 +492,13 @@ public class LogFactoryImpl extends LogFactory { return logAdapter; } + // ------------------------------------------------------ + // Static Methods + // + // These methods only defined as workarounds for a java 1.2 bug; + // theoretically none of these are needed. + // ------------------------------------------------------ + /** * Attempts to create a Log instance for the given category name. * Follows the discovery process described in the class javadoc. @@ -577,13 +592,6 @@ public class LogFactoryImpl extends LogFactory { return result; } - // ------------------------------------------------------ - // Static Methods - // - // These methods only defined as workarounds for a java 1.2 bug; - // theoretically none of these are needed. - // ------------------------------------------------------ - /** * Checks system properties and the attribute map for * a Log implementation specified by the user under the @@ -656,6 +664,8 @@ public class LogFactoryImpl extends LogFactory { return attributes.get(name); } + // ------------------------------------------------------ Protected Methods + /** * Return an array containing the names of all currently defined * configuration attributes. If there are no such attributes, a zero @@ -666,8 +676,6 @@ public class LogFactoryImpl extends LogFactory { return (String[]) attributes.keySet().toArray(EMPTY_STRING_ARRAY); } - // ------------------------------------------------------ Protected Methods - /** * Return the classloader from which we should try to load the logging * adapter classes. @@ -756,6 +764,7 @@ public class LogFactoryImpl extends LogFactory { return Boolean.parseBoolean(val); } + /** * Attempt to find an attribute (see method setAttribute) or a * system property with the provided name and return its value. @@ -813,7 +822,6 @@ public class LogFactoryImpl extends LogFactory { return null; } - /** * Convenience method to derive a name from the specified class and * call {@code getInstance(String)} with it. @@ -898,6 +906,8 @@ public class LogFactoryImpl extends LogFactory { return logConstructor; } + // ------------------------------------------------------ Private Methods + /** * Given two related classloaders, return the one which is a child of * the other. @@ -944,8 +954,6 @@ public class LogFactoryImpl extends LogFactory { return null; } - // ------------------------------------------------------ Private Methods - /** * Fetch the parent classloader of a specified classloader. *