Skip to content

Fix NPE in RollingFileManger #2114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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://github.com/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://github.com/apache/logging-log4j2/pull/1645"/>
<description format="asciidoc">
Fix NPE in `RollingFileManager`.
</description>
</entry>