You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2022/12/14 19:22:41 UTC
[sis] 02/03: Fix an integer overflow when the filename is the root directory or an empty path.
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
commit 56212495a059cc019f52fe39c5680124fa5ed3d1
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Wed Dec 14 16:00:49 2022 +0100
Fix an integer overflow when the filename is the root directory or an empty path.
---
.../org/apache/sis/internal/storage/io/IOUtilities.java | 13 ++++++++-----
.../org/apache/sis/internal/storage/io/package-info.java | 2 +-
.../org/apache/sis/internal/storage/io/IOUtilitiesTest.java | 4 +++-
3 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/IOUtilities.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/IOUtilities.java
index 3df8039a40..fba5a3cd8d 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/IOUtilities.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/IOUtilities.java
@@ -59,7 +59,7 @@ import org.apache.sis.internal.storage.Resources;
*
* @author Martin Desruisseaux (Geomatys)
* @author Johann Sorel (Geomatys)
- * @version 1.2
+ * @version 1.4
* @since 0.3
* @module
*/
@@ -95,6 +95,7 @@ public final class IOUtilities extends Static {
* instance. If the given argument is specialized type like {@code Path} or {@code File}, then this method uses
* dedicated API like {@link Path#getFileName()}. Otherwise this method gets a string representation of the path
* and returns the part after the last {@code '/'} or platform-dependent name separator character, if any.
+ * The returned string may be empty if the given path is empty or is the root directory.
*
* @param path the path as an instance of one of the above-cited types, or {@code null}.
* @return the filename in the given path, or {@code null} if the given object is null or of unknown type.
@@ -149,13 +150,15 @@ public final class IOUtilities extends Static {
*/
end = name.length();
do {
- fromIndex = name.lastIndexOf('/', --end) + 1;
+ if (--end < 0) return ""; // `end` is temporarily inclusive in this loop.
+ fromIndex = name.lastIndexOf('/', end);
if (separator != '/') {
// Search for platform-specific character only if the object is neither a URL or a URI.
- fromIndex = Math.max(fromIndex, CharSequences.lastIndexOf(name, separator, fromIndex, end+1) + 1);
+ fromIndex = Math.max(fromIndex, name.lastIndexOf(separator, end));
}
- } while (fromIndex > end);
- end++;
+ } while (fromIndex == end); // Continue if '/' is the last character.
+ fromIndex++; // Character after the '/' separator.
+ end++; // Make exclusive.
}
if (extension) {
fromIndex = CharSequences.lastIndexOf(name, '.', fromIndex, end) + 1;
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/package-info.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/package-info.java
index de0e699a69..c3f8ad2eae 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/package-info.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/package-info.java
@@ -24,7 +24,7 @@
* may change in incompatible ways in any future version without notice.
*
* @author Martin Desruisseaux (Geomatys)
- * @version 1.3
+ * @version 1.4
* @since 0.3
* @module
*/
diff --git a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/IOUtilitiesTest.java b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/IOUtilitiesTest.java
index 4c2483388c..af7c998a7b 100644
--- a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/IOUtilitiesTest.java
+++ b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/IOUtilitiesTest.java
@@ -35,7 +35,7 @@ import static org.junit.Assert.*;
*
* @author Martin Desruisseaux (Geomatys)
* @author Johann Sorel (Geomatys)
- * @version 1.2
+ * @version 1.4
* @since 0.3
* @module
*/
@@ -53,6 +53,8 @@ public final strictfp class IOUtilitiesTest extends TestCase {
assertEquals("Map.png", IOUtilities.filename(new URI ("file:/Users/name/Map.png")));
assertEquals("Map.png", IOUtilities.filename(new URL ("file:/Users/name/Map.png")));
assertEquals("name", IOUtilities.filename(new URI ("file:/Users/name/")));
+ assertEquals("", IOUtilities.filename("/"));
+ assertEquals("", IOUtilities.filename(""));
assertNull(IOUtilities.filename(Boolean.FALSE));
assertNull(IOUtilities.filename(null));
}