Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/org/apache/maven/shared/utils/PathTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,16 @@ public static String getRelativeFilePath(final String oldPath, final String newP
String toPath = new File(newPath).getPath();

// strip any leading slashes if its a windows path
if (toPath.matches("^\\[a-zA-Z]:")) {
if (toPath.length() > 2
&& toPath.charAt(0) == '\\'
&& Character.isLetter(toPath.charAt(1))
&& toPath.charAt(2) == ':') {
toPath = toPath.substring(1);
}
if (fromPath.matches("^\\[a-zA-Z]:")) {
if (fromPath.length() > 2
&& fromPath.charAt(0) == '\\'
&& Character.isLetter(fromPath.charAt(1))
&& fromPath.charAt(2) == ':') {
fromPath = fromPath.substring(1);
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/apache/maven/shared/utils/PathToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ public void testGetRelativePath2Parm() {
assertEquals("", PathTool.getRelativePath("/usr/local/java/bin/java.sh", "/usr/local/"));
}

@Test
public void testGetRelativeFilePathWithDifferentWindowsDrives() {
// Tests that the regex fix at line 146 correctly strips a leading backslash
// before a Windows drive letter. The old regex "^\\[a-zA-Z]:" matched the
// literal string "[a-zA-Z]:" instead of a backslash + drive letter.
// Different drives with leading backslash should return null.
assertNull(PathTool.getRelativeFilePath("\\C:\\usr\\local", "\\D:\\usr\\local\\java\\bin"));
}

@Test
public void testUppercaseDrive() {
assertNull(PathTool.uppercaseDrive(null));
Expand Down
Loading