Skip to content

PathTool.getRelativeFilePath: fix broken Windows drive-letter regex#384

Open
elharo wants to merge 3 commits into
masterfrom
fix/pathtool-windows-drive-regex
Open

PathTool.getRelativeFilePath: fix broken Windows drive-letter regex#384
elharo wants to merge 3 commits into
masterfrom
fix/pathtool-windows-drive-regex

Conversation

@elharo

@elharo elharo commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

The regex ^\\[a-zA-Z]: at lines 146 and 149 had two bugs:

  1. The \\[ matched a literal [ character, not a backslash, because [ was escaped by \
  2. String.matches() requires the entire string to match the pattern, but this regex was intended to match only the leading \C: prefix — so it never matched any real path

The Windows drive-letter normalization code was effectively dead. Paths like \C:\foo (produced by File.getPath() on Windows) were never getting their leading \ stripped, causing the drive-letter comparison logic at lines 154–175 to be skipped. This could produce incorrect relative paths or fail to detect mismatched drives (returning a path instead of null).

Fix: Replaced the broken regex with a simple character-level check:

if (toPath.length() > 2 && toPath.charAt(0) == '\\'
        && Character.isLetter(toPath.charAt(1)) && toPath.charAt(2) == ':')

Fixes #383

@elharo elharo added the bug Something isn't working label Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PathTool.getRelativeFilePath broken Windows drive letter regex

1 participant