Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8360028

(fs) Path.relativize throws StringIndexOutOfBoundsException (win)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 24.0.1
    • core-libs
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11, Java 24.0.1

      A DESCRIPTION OF THE PROBLEM :
      Certain Windows symbolic links will trigger a StringIndexOutOfBoundsException when calling WindowsPath.relativize().

      ```
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
      at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
      at java.base/java.lang.String.checkIndex(String.java:4917)
      at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:46)
      at java.base/java.lang.String.charAt(String.java:1616)
      at java.base/sun.nio.fs.WindowsPath.normalize(WindowsPath.java:528)
      at java.base/sun.nio.fs.WindowsPath.relativize(WindowsPath.java:424)
      at java.base/sun.nio.fs.WindowsPath.relativize(WindowsPath.java:42)
      ```

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - Create the directory `C:\Temp\dir`.
      - Create the directory `C:\Temp\other\dir`.
      - In `C:\Temp\other\dir`, execute the command `mklink /D link "..\\..\\dir"`.

      This should result in the following file system contents:

      ```
      c:\Temp\other\dir>dir
       Volume in drive C has no label.
       Volume Serial Number is 368F-72D1

       Directory of c:\Temp\other\dir

      19/06/2025 13:28 <DIR> .
      19/06/2025 13:27 <DIR> ..
      19/06/2025 13:28 <SYMLINKD> link [..\\..\\dir]
      ```

      Note that although the use of `\\` might be uncommon, Windows can still handle it properly, and operations like `cd link` will function as expected.

      Now, run the following code snippet to trigger the StringIndexOutOfBoundsException:

      ```
      public static void main(String[] args) throws IOException {
      final Path linkPath = Path.of("C:\\temp\\other\\dir\\link");
      final Path link = Files.readSymbolicLink(linkPath);
      final Path badRoot = linkPath.getParent().resolve(link);
      final Path badFile = badRoot.resolve("file");
      badRoot.relativize(badFile);
      }
      ```


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No crash.
      ACTUAL -
      Crash

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) throws IOException {
      final Path linkPath = Path.of("C:\\temp\\other\\dir\\link");
      final Path link = Files.readSymbolicLink(linkPath);
      final Path badRoot = linkPath.getParent().resolve(link);
      final Path badFile = badRoot.resolve("file");
      badRoot.relativize(badFile);
      }

      ---------- END SOURCE ----------

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: