This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 5847309  SLING-12754 fix NPE in error handling (#57)
5847309 is described below

commit 5847309351f65e5f0c9143e3e297bd93e3c332de
Author: Jörg Hoh <[email protected]>
AuthorDate: Wed Apr 23 09:34:17 2025 +0200

    SLING-12754 fix NPE in error handling (#57)
---
 .../java/org/apache/sling/api/resource/ResourceUtil.java     | 12 +++++++++---
 .../java/org/apache/sling/api/resource/ResourceUtilTest.java |  4 ++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/sling/api/resource/ResourceUtil.java 
b/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
index fbe2ee7..9c2f816 100644
--- a/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
+++ b/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Objects;
 
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 import org.jetbrains.annotations.NotNull;
@@ -271,18 +272,23 @@ public class ResourceUtil {
      * @throws NullPointerException If <code>path</code> is <code>null</code>.
      */
     public static @NotNull String getName(@NotNull String path) {
+        Objects.requireNonNull(path, "provided path is null");
         if ("/".equals(path)) {
             return "";
         }
 
         // normalize path (remove . and ..)
-        path = normalize(path);
-        if ("/".equals(path)) {
+        final String normalizedPath = normalize(path);
+        if (normalizedPath == null) {
+            throw new IllegalArgumentException(
+                    String.format("normalizing path '%s' resolves to a path 
higher than root", path));
+        }
+        if ("/".equals(normalizedPath)) {
             return "";
         }
 
         // find the last slash
-        return path.substring(path.lastIndexOf('/') + 1);
+        return normalizedPath.substring(normalizedPath.lastIndexOf('/') + 1);
     }
 
     /**
diff --git a/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java 
b/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
index 7716050..8826020 100644
--- a/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
+++ b/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
@@ -32,6 +32,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.mock;
@@ -181,6 +182,9 @@ public class ResourceUtilTest {
         assertEquals("b", ResourceUtil.getName("b/c/.."));
         assertEquals("b", ResourceUtil.getName("/b/c/.."));
         assertEquals("", ResourceUtil.getName("/b/c/../.."));
+
+        assertThrows(NullPointerException.class, () -> 
ResourceUtil.getName((String) null));
+        assertThrows(IllegalArgumentException.class, () -> 
ResourceUtil.getName("/b/c/../../../a"));
     }
 
     @Test

Reply via email to