Skip to content

Commit e58a4da

Browse files
committed
8216363: NullPointerException in java.util.logging.Handler#isLoggable
The implementation is changed to match the spec: isLoggable(null) returns false. Reviewed-by: mchung, lancea
1 parent 7fd7a11 commit e58a4da

4 files changed

Lines changed: 103 additions & 6 deletions

File tree

src/java.logging/share/classes/java/util/logging/Handler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -329,12 +329,13 @@ public Level getLevel() {
329329
* handler from logging the {@code LogRecord}. It will return false if
330330
* the {@code LogRecord} is null.
331331
*
332-
* @param record a {@code LogRecord}
332+
* @param record a {@code LogRecord} (may be null).
333333
* @return true if the {@code LogRecord} would be logged.
334334
*
335335
*/
336336
public boolean isLoggable(LogRecord record) {
337337
final int levelValue = getLevel().intValue();
338+
if (record == null) return false;
338339
if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
339340
return false;
340341
}

src/java.logging/share/classes/java/util/logging/MemoryHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -270,7 +270,7 @@ public Level getPushLevel() {
270270
* check whether the {@code LogRecord} would result in a "push" of the
271271
* buffer contents. It will return false if the {@code LogRecord} is null.
272272
*
273-
* @param record a {@code LogRecord}
273+
* @param record a {@code LogRecord} (may be null).
274274
* @return true if the {@code LogRecord} would be logged.
275275
*
276276
*/

src/java.logging/share/classes/java/util/logging/StreamHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -225,7 +225,7 @@ public synchronized void publish(LogRecord record) {
225225
* whether it satisfies any {@code Filter}. It will also return false if
226226
* no output stream has been assigned yet or the LogRecord is null.
227227
*
228-
* @param record a {@code LogRecord}
228+
* @param record a {@code LogRecord} (may be null).
229229
* @return true if the {@code LogRecord} would be logged.
230230
*
231231
*/
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8216363
27+
* @summary Test that Handler.isLoggable(null) returns false
28+
* @run main/othervm IsLoggableHandlerTest
29+
*/
30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.util.UUID;
33+
import java.util.logging.ConsoleHandler;
34+
import java.util.logging.FileHandler;
35+
import java.util.logging.Handler;
36+
import java.util.logging.Level;
37+
import java.util.logging.LogRecord;
38+
import java.util.logging.MemoryHandler;
39+
import java.util.logging.StreamHandler;
40+
import java.util.stream.Stream;
41+
42+
public class IsLoggableHandlerTest {
43+
44+
45+
public static void main(String... args) throws IOException {
46+
String userDir = System.getProperty("user.dir", ".");
47+
File logfile = new File(userDir, "IsLoggableHandlerTest_" + UUID.randomUUID() + ".log");
48+
try {
49+
System.out.println("Dummy logfile: " + logfile.getAbsolutePath());
50+
Handler h = new CustomHandler();
51+
testIsLoggable(h);
52+
testIsLoggable(new MemoryHandler(h, 1, Level.ALL));
53+
testIsLoggable(new StreamHandler(System.out, new java.util.logging.SimpleFormatter()));
54+
testIsLoggable(new FileHandler(logfile.getAbsolutePath()));
55+
testIsLoggable(new ConsoleHandler());
56+
} finally {
57+
if (logfile.canRead()) {
58+
try {
59+
System.out.println("Deleting dummy logfile: " + logfile.getAbsolutePath());
60+
logfile.delete();
61+
} catch (Throwable t) {
62+
System.out.println("Warning: failed to delete dummy logfile: " + t);
63+
t.printStackTrace();
64+
}
65+
}
66+
}
67+
}
68+
69+
public static void testIsLoggable(Handler h) {
70+
System.out.println("Testing " + h.getClass().getName());
71+
// should not throw NPE but return false
72+
if (h.isLoggable(null)) {
73+
throw new AssertionError(h.getClass().getName()
74+
+ ": null record should not be loggable");
75+
}
76+
h.setLevel(Level.ALL);
77+
// should still not throw NPE but return false
78+
if (h.isLoggable(null)) {
79+
throw new AssertionError(h.getClass().getName()
80+
+ ": null record should not be loggable");
81+
}
82+
// should not throw NPE
83+
h.publish(null);
84+
}
85+
86+
public static final class CustomHandler extends Handler {
87+
@Override
88+
public void publish(LogRecord record) { }
89+
@Override
90+
public void flush() { }
91+
@Override
92+
public void close() throws SecurityException { }
93+
}
94+
95+
96+
}

0 commit comments

Comments
 (0)