Skip to content

Commit

Permalink
Fix NPE in RollingFileManger
Browse files Browse the repository at this point in the history
This PR fixes a `NullPointerException` in
`RollingFileManager#createParentDir`.

Closes #1645
  • Loading branch information
ppkarwasz committed Dec 21, 2023
1 parent cf147fd commit 95a90e0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
*/
package org.apache.logging.log4j.core.appender.rolling;

import java.io.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
Expand All @@ -26,8 +36,8 @@
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.util.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junitpioneer.jupiter.Issue;

public class RollingFileManagerTest {

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

Assert.assertNotNull(appender);
assertNotNull(appender);
final String testContent = "Test";
try (final RollingFileManager manager = appender.getManager()) {
Assert.assertEquals(file.getAbsolutePath(), manager.getFileName());
assertEquals(file.getAbsolutePath(), manager.getFileName());
manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
}
try (final Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.US_ASCII)) {
Assert.assertEquals(testContent, IOUtils.toString(reader));
assertEquals(testContent, IOUtils.toString(reader));
}
}
}
Expand Down Expand Up @@ -125,7 +135,7 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
null,
null,
configuration);
Assert.assertNotNull(manager);
assertNotNull(manager);
manager.initialize();

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

// If the rollover fails, then the size should not be reset
Assert.assertNotEquals(0, manager.getFileSize());
assertNotEquals(0, manager.getFileSize());

// The initialTime should not have changed
Assert.assertEquals(initialTime, manager.getFileTime());
assertEquals(initialTime, manager.getFileTime());
}

@Test
@Issue("https:/apache/logging-log4j2/issues/1645")
public void testCreateParentDir() {
final Configuration configuration = new NullConfiguration();
final RollingFileManager manager = RollingFileManager.getFileManager(
null,
"testCreateParentDir.log.%d{yyyy-MM-dd}",
true,
false,
NoOpTriggeringPolicy.INSTANCE,
DirectWriteRolloverStrategy.newBuilder()
.withConfig(configuration)
.build(),
null,
PatternLayout.createDefaultLayout(configuration),
0,
true,
true,
null,
null,
null,
configuration);
assertNotNull(manager);
try {
final File file = new File("file_in_current_dir.log");
assertNull(file.getParentFile());
manager.createParentDir(file);
} catch (final Throwable t) {
fail("createParentDir failed: " + t.getMessage());
} finally {
manager.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ public String getFileName() {
@Override
protected void createParentDir(File file) {
if (directWrite) {
file.getParentFile().mkdirs();
final File parent = file.getParentFile();
// If the parent is null the file is in the current working directory.
if (parent != null) {
parent.mkdirs();
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/fix_create_parent_dir.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
type="fixed">
<issue id="1645" link="https:/apache/logging-log4j2/pull/1645"/>
<description format="asciidoc">
Fix NPE in `RollingFileManager`.
</description>
</entry>

0 comments on commit 95a90e0

Please sign in to comment.