You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/30 11:37:05 UTC
[commons-compress] branch master updated: Avoid NPE in FileNameUtils.getBaseName(Path) for paths with zero elements like root paths
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new 36ac1740 Avoid NPE in FileNameUtils.getBaseName(Path) for paths with zero elements like root paths
36ac1740 is described below
commit 36ac1740fa32895c40af69c500524a5f551e4c7d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Nov 30 06:37:00 2022 -0500
Avoid NPE in FileNameUtils.getBaseName(Path) for paths with zero
elements like root paths
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/compress/utils/FileNameUtils.java | 3 ++-
.../org/apache/commons/compress/utils/FileNameUtilsTest.java | 10 ++++++++++
3 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1764288c..107d5305 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,6 +45,7 @@ The <action> type attribute can be add,update,fix,remove.
<release version="1.23" date="not released">
<!-- FIX -->
<action type="update" dev="ggregory" due-to="CodeQL, Gary Gregory">Implicit narrowing conversion in compound assignment.</action>
+ <action type="update" dev="ggregory" due-to="CodeQL, Gary Gregory">Avoid NPE in FileNameUtils.getBaseName(Path) for paths with zero elements like root paths.</action>
<!-- ADD -->
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot">Bump mockito.version from 4.8.0 to 4.9.0 #328, #331.</action>
diff --git a/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java b/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java
index 127de93a..c6738137 100644
--- a/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java
+++ b/src/main/java/org/apache/commons/compress/utils/FileNameUtils.java
@@ -51,7 +51,8 @@ public class FileNameUtils {
if (path == null) {
return null;
}
- return fileNameToBaseName(path.getFileName().toString());
+ final Path fileName = path.getFileName();
+ return fileName != null ? fileNameToBaseName(fileName.toString()) : null;
}
/**
diff --git a/src/test/java/org/apache/commons/compress/utils/FileNameUtilsTest.java b/src/test/java/org/apache/commons/compress/utils/FileNameUtilsTest.java
index f100e3c4..b0edcc3a 100644
--- a/src/test/java/org/apache/commons/compress/utils/FileNameUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/FileNameUtilsTest.java
@@ -21,9 +21,11 @@ package org.apache.commons.compress.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
+import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.Test;
public class FileNameUtilsTest {
@@ -38,6 +40,14 @@ public class FileNameUtilsTest {
public void getBaseNamePathBaseCases() {
assertEquals("bar", FileNameUtils.getBaseName(Paths.get("a/b/c/bar.foo")));
assertEquals("foo", FileNameUtils.getBaseName(Paths.get("foo")));
+ assertEquals("", FileNameUtils.getBaseName(Paths.get("")));
+ assertEquals("", FileNameUtils.getBaseName(Paths.get(".")));
+ for (File f : File.listRoots()) {
+ assertEquals(null, FileNameUtils.getBaseName(f.toPath()));
+ }
+ if (SystemUtils.IS_OS_WINDOWS) {
+ assertEquals(null, FileNameUtils.getBaseName(Paths.get("C:\\")));
+ }
}
@Test