Skip to content

Commit

Permalink
fix issue 62
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Mar 22, 2023
1 parent db444fa commit 7d73278
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 18 deletions.
42 changes: 28 additions & 14 deletions src/main/java/org/apache/log4j/helpers/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.net.URL;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.io.InterruptedIOException;

/**
* Load resources (or images) from various sources.
Expand Down Expand Up @@ -56,7 +55,9 @@ public static URL getResource(String resource, Class clazz) {
/**
* This method will search for <code>resource</code> in different places. The search order is as follows:
* <ol>
*
* <li><p>Search for <code>resource</code> using the thread context class loader,
* unless "log4j.ignoreTCL" system property was set to true.</p>
* </li>
* <li><p>Search for <code>resource</code> using the class
* loader that loaded this class (<code>Loader</code>).<p>
* </li>
Expand All @@ -67,31 +68,35 @@ public static URL getResource(String resource, Class clazz) {
*
* <p>Nota bene: In versions of reload4j 1.2.23 and earlier, the jaadoc documentation stated that

This comment has been minimized.

Copy link
@davidmoten

davidmoten Mar 24, 2023

Not in commit, jaadoc -> javadoc

* the thread context class loader was used but when running under JDK 9 and later this
* was <b>not</b> actually the case. As of version 1.2.24, the javadoc above matches the code as executed.
* was <b>not</b> actually the case. As of version 1.2.25, the javadoc corresponds to the original
* intention as documented.
* </p>
*
*
* @param resource the resource to load
*/
static public URL getResource(String resource) {
ClassLoader classLoader = null;
URL url = null;

try {
// We could not find resource. Ler us now try with the
// classloader that loaded this class.
classLoader = Loader.class.getClassLoader();
if (classLoader != null) {
LogLog.debug("Trying to find [" + resource + "] using " + classLoader + " class loader.");
url = classLoader.getResource(resource);
if (url != null) {
return url;
}
// unless intsructed to ignore the TCL, try getting the resource using TCL, return if found.

This comment has been minimized.

Copy link
@davidmoten

davidmoten Mar 24, 2023

instructed

if(!ignoreTCL) {
URL url0 = innerGetResource(resource, getTCL());
if(url0 != null) { return url0; }
}

// if we were instructed to ignore TCL or if no url was dound, try using the
// class loader that loaded this class.
URL url = innerGetResource(resource, Loader.class.getClassLoader());
if(url != null) { return url; }

} catch (SecurityException t) {
// can't be InterruptedException or InterruptedIOException
// since not declared, must be error or RuntimeError.
LogLog.warn(TSTR, t);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

// Last ditch attempt: get the resource from the class path. It
Expand All @@ -102,6 +107,15 @@ static public URL getResource(String resource) {
return ClassLoader.getSystemResource(resource);
}

private static URL innerGetResource(String resource, ClassLoader classLoader) {
if (classLoader != null) {
LogLog.debug("Trying to find [" + resource + "] using " + classLoader + " class loader.");
return classLoader.getResource(resource);
} else {
return null;
}
}

/**
* Are we running under JDK 1.x?
* @deprecated with no replacement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.log4j.testUtil;
package org.apache.log4j.helpers;

public class VersionUtil {
static final int DEFAULT_GUESS = 8;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/log4j/spi/LoggingEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,9 @@ public Set getPropertyKeySet() {
*/
public Map getProperties() {
getMDCCopy();
Map properties;
Map properties; // local variable
if (mdcCopy == null) {
properties = new HashMap();
properties = new HashMap(); // local variable not otherwise used within the event
} else {
properties = mdcCopy;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/apache/log4j/MDCTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.lang.ref.Reference;
import java.lang.reflect.Field;

import org.apache.log4j.testUtil.VersionUtil;
import org.apache.log4j.helpers.VersionUtil;

import junit.framework.TestCase;

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/apache/log4j/helpers/VersionUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.apache.log4j.helpers;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class VersionUtilTest {

@Test
public void smoke() {
assertEquals(4, VersionUtil.getJavaMajorVersion("1.4.xx"));
assertEquals(5, VersionUtil.getJavaMajorVersion("1.5"));
assertEquals(5, VersionUtil.getJavaMajorVersion("1.5.xx"));
assertEquals(5, VersionUtil.getJavaMajorVersion("1.5AA"));
assertEquals(7, VersionUtil.getJavaMajorVersion("1.7.0_80-b15"));
assertEquals(8, VersionUtil.getJavaMajorVersion("1.8.0_311"));
assertEquals(9, VersionUtil.getJavaMajorVersion("9EA"));
assertEquals(9, VersionUtil.getJavaMajorVersion("9.0.1"));
assertEquals(18, VersionUtil.getJavaMajorVersion("18.3+xx"));
assertEquals(19, VersionUtil.getJavaMajorVersion("19"));

}
}
116 changes: 116 additions & 0 deletions src/test/java/org/apache/log4j/net/HashmapDOSTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.apache.log4j.net;


import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Hashtable;

@Ignore
public class HashmapDOSTest {

Hashtable<Object, Object> ht = new Hashtable<Object, Object>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;

ObjectInputStream ois;
int maxHashMapCapacity = getMaxHashMapCapacityValue();

@Before
public void beforeEach() throws IOException {
oos = new ObjectOutputStream(baos);
}
@Test
public void smokeHashMap() throws Exception {
HashMap<Object, Object> ht = createDeepMap(null, 10);
setSizeUsingReflection(ht, maxHashMapCapacity);
oos.writeObject(ht);
oos.flush();
oos.close();

byte[] byteArray = baos.toByteArray();

System.out.println("byteArray,length="+byteArray.length);


ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bais);

Object o = ois.readObject();


}
@Test
public void smokeHashtable() throws Exception {
Hashtable ht = new Hashtable();
ht.put("1", "1");
oos.writeObject(ht);
oos.flush();
oos.close();

byte[] byteArray = baos.toByteArray();

System.out.println("byteArray,length="+byteArray.length);


ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bais);

Hashtable htback = (Hashtable) ois.readObject();

System.out.println(htback.get("1"));

}

private static void setSizeUsingReflection(HashMap map, int size) throws Exception {
Field sizeField = HashMap.class.getDeclaredField("size");
sizeField.setAccessible(true);

while (map != null) {
sizeField.set(map, size);
map = (HashMap) map.keySet().iterator().next();
}
}

static int getMaxHashMapCapacityValue() {
Field maxHashMapCapacityField = null;
try {
maxHashMapCapacityField = HashMap.class.getDeclaredField("MAXIMUM_CAPACITY");
maxHashMapCapacityField.setAccessible(true);

int maxHashMapCapacity = maxHashMapCapacityField.getInt(null);
return maxHashMapCapacity;
} catch (NoSuchFieldException e) {
throw new RuntimeException("Unable to obtain field HashMap.MAXIMUM_CAPACITY", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Unable to read HashMap.MAXIMUM_CAPACITY", e);
}
}


private static HashMap createDeepMap(HashMap child, int depth) {
if (child == null) {
child = new HashMap();
// add one last element so that buffer is flushed
child.put(null, null);
}

if (depth <= 1) {
return child;
}

HashMap parent = new HashMap();
parent.put(child, null);

return createDeepMap(parent, depth - 1);
}

}

0 comments on commit 7d73278

Please sign in to comment.