Skip to content

Commit 95a90e0

Browse files
committed
Fix NPE in RollingFileManger
This PR fixes a `NullPointerException` in `RollingFileManager#createParentDir`. Closes #1645
1 parent cf147fd commit 95a90e0

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.rolling;
1818

19-
import java.io.*;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.fail;
24+
25+
import java.io.File;
26+
import java.io.FileInputStream;
27+
import java.io.IOException;
28+
import java.io.InputStreamReader;
29+
import java.io.Reader;
2030
import java.nio.charset.StandardCharsets;
2131
import org.apache.logging.log4j.core.LoggerContext;
2232
import org.apache.logging.log4j.core.appender.RollingFileAppender;
@@ -26,8 +36,8 @@
2636
import org.apache.logging.log4j.core.layout.PatternLayout;
2737
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
2838
import org.apache.logging.log4j.core.util.IOUtils;
29-
import org.junit.Assert;
3039
import org.junit.Test;
40+
import org.junitpioneer.jupiter.Issue;
3141

3242
public class RollingFileManagerTest {
3343

@@ -75,14 +85,14 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
7585
.withPolicy(new SizeBasedTriggeringPolicy(100))
7686
.build();
7787

78-
Assert.assertNotNull(appender);
88+
assertNotNull(appender);
7989
final String testContent = "Test";
8090
try (final RollingFileManager manager = appender.getManager()) {
81-
Assert.assertEquals(file.getAbsolutePath(), manager.getFileName());
91+
assertEquals(file.getAbsolutePath(), manager.getFileName());
8292
manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
8393
}
8494
try (final Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.US_ASCII)) {
85-
Assert.assertEquals(testContent, IOUtils.toString(reader));
95+
assertEquals(testContent, IOUtils.toString(reader));
8696
}
8797
}
8898
}
@@ -125,7 +135,7 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
125135
null,
126136
null,
127137
configuration);
128-
Assert.assertNotNull(manager);
138+
assertNotNull(manager);
129139
manager.initialize();
130140

131141
// Get the initialTime of this original log file
@@ -139,9 +149,43 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
139149
manager.rollover();
140150

141151
// If the rollover fails, then the size should not be reset
142-
Assert.assertNotEquals(0, manager.getFileSize());
152+
assertNotEquals(0, manager.getFileSize());
143153

144154
// The initialTime should not have changed
145-
Assert.assertEquals(initialTime, manager.getFileTime());
155+
assertEquals(initialTime, manager.getFileTime());
156+
}
157+
158+
@Test
159+
@Issue("https://github.com/apache/logging-log4j2/issues/1645")
160+
public void testCreateParentDir() {
161+
final Configuration configuration = new NullConfiguration();
162+
final RollingFileManager manager = RollingFileManager.getFileManager(
163+
null,
164+
"testCreateParentDir.log.%d{yyyy-MM-dd}",
165+
true,
166+
false,
167+
NoOpTriggeringPolicy.INSTANCE,
168+
DirectWriteRolloverStrategy.newBuilder()
169+
.withConfig(configuration)
170+
.build(),
171+
null,
172+
PatternLayout.createDefaultLayout(configuration),
173+
0,
174+
true,
175+
true,
176+
null,
177+
null,
178+
null,
179+
configuration);
180+
assertNotNull(manager);
181+
try {
182+
final File file = new File("file_in_current_dir.log");
183+
assertNull(file.getParentFile());
184+
manager.createParentDir(file);
185+
} catch (final Throwable t) {
186+
fail("createParentDir failed: " + t.getMessage());
187+
} finally {
188+
manager.close();
189+
}
146190
}
147191
}

log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,11 @@ public String getFileName() {
356356
@Override
357357
protected void createParentDir(File file) {
358358
if (directWrite) {
359-
file.getParentFile().mkdirs();
359+
final File parent = file.getParentFile();
360+
// If the parent is null the file is in the current working directory.
361+
if (parent != null) {
362+
parent.mkdirs();
363+
}
360364
}
361365
}
362366

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://logging.apache.org/log4j/changelog"
4+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
5+
type="fixed">
6+
<issue id="1645" link="https://github.com/apache/logging-log4j2/pull/1645"/>
7+
<description format="asciidoc">
8+
Fix NPE in `RollingFileManager`.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)