Blob Blame History Raw
From 7e1c46bc8521540627e9a9d870743f282c87becb Mon Sep 17 00:00:00 2001
From: Marian Koncek <mkoncek@redhat.com>
Date: Thu, 2 Jul 2020 12:01:38 +0200
Subject: [PATCH] Fix NPE in MailHandler-JDK-8216363

---
 .../sun/mail/util/logging/MailHandler.java    | 11 +++-
 .../mail/util/logging/MailHandlerTest.java    | 59 +++++++++++--------
 2 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/mail/src/main/java/com/sun/mail/util/logging/MailHandler.java b/mail/src/main/java/com/sun/mail/util/logging/MailHandler.java
index e6879ba..2021c99 100644
--- a/mail/src/main/java/com/sun/mail/util/logging/MailHandler.java
+++ b/mail/src/main/java/com/sun/mail/util/logging/MailHandler.java
@@ -594,6 +594,9 @@ public class MailHandler extends Handler {
      */
     @Override
     public boolean isLoggable(final LogRecord record) {
+        if (record == null) { //JDK-8233979
+            return false;
+        }
         int levelValue = getLevel().intValue();
         if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
             return false;
@@ -634,8 +637,12 @@ public class MailHandler extends Handler {
         if (tryMutex()) {
             try {
                 if (isLoggable(record)) {
-                    record.getSourceMethodName(); //Infer caller.
-                    publish0(record);
+                    if (record != null) {
+                        record.getSourceMethodName(); //Infer caller.
+                        publish0(record);
+                    } else { //Override of isLoggable is broken.
+                        reportNullError(ErrorManager.WRITE_FAILURE);
+                    }
                 }
             } catch (final LinkageError JDK8152515) {
                 reportLinkageError(JDK8152515, ErrorManager.WRITE_FAILURE);
diff --git a/mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java b/mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java
index 506b3d6..19541b7 100644
--- a/mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java
+++ b/mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java
@@ -474,34 +474,15 @@ public class MailHandlerTest extends AbstractLogging {
         instance.setErrorManager(em);
 
         instance.setLevel(lvl);
-        MemoryHandler mem = null;
         boolean result = false;
         boolean expect = true;
-        try {
-            result = instance.isLoggable(record);
-            mem = new MemoryHandler(new ConsoleHandler(), 100, Level.OFF);
-            mem.setErrorManager(em);
-            mem.setLevel(lvl);
-            expect = mem.isLoggable(record);
-        } catch (RuntimeException mailEx) {
-            try {
-                if (mem != null) {
-                    fail("MemoryHandler threw and exception: " + mailEx);
-                } else {
-                    mem = new MemoryHandler(new ConsoleHandler(), 100, Level.OFF);
-                    mem.setErrorManager(em);
-                    mem.setLevel(lvl);
-                    expect = mem.isLoggable(record);
-                    fail("MailHandler threw and exception: " + mailEx);
-                }
-            } catch (RuntimeException memEx) {
-                assertEquals(memEx.getClass(), mailEx.getClass());
-                result = false;
-                expect = false;
-            }
+        if (record == null || record.getLevel().intValue() < lvl.intValue() 
+        		|| Level.OFF.intValue() == lvl.intValue()) {
+            expect = false;
         }
-        assertEquals(expect, result);
 
+        result = instance.isLoggable(record);
+        assertEquals(lvl.getName(), expect, result);
         instance.setLevel(Level.INFO);
         instance.setFilter(BooleanFilter.FALSE);
         instance.setAttachmentFormatters(
@@ -643,6 +624,36 @@ public class MailHandlerTest extends AbstractLogging {
         instance.close();
     }
 
+    @Test
+    public void testPublishNull() {
+    	MailHandler instance = new MailHandler();
+    	InternalErrorManager em = new InternalErrorManager();
+    	instance.setErrorManager(em);
+    	instance.setLevel(Level.ALL);
+    	instance.publish((LogRecord) null);
+    	instance.close();
+    	for (Throwable t : em.exceptions) {
+            dump(t);
+        }
+        assertTrue(em.exceptions.isEmpty());
+    }
+
+    @Test
+    public void testPublishNullAsTrue() {
+    	MailHandler instance = new MailHandler() {
+    		public boolean isLoggable(LogRecord r) {
+    			return true;
+    		}
+    	};
+    	InternalErrorManager em = new InternalErrorManager();
+    	instance.setErrorManager(em);
+    	instance.setLevel(Level.ALL);
+    	instance.publish((LogRecord) null);
+    	instance.close();
+    	assertEquals(true, em.exceptions.get(0) instanceof NullPointerException);
+        assertEquals(1, em.exceptions.size());
+    }
+
     @Test
     public void testPublishLinkageError() throws Exception {
         testLinkageErrorWithStack("publish");
-- 
2.25.4