Skip to content

8191963: Path.equals() and File.equals() return true for two different files on Windows #25788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 17 additions & 1 deletion src/java.base/windows/classes/java/io/WinNTFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,23 @@ public int getNameMax(String path) {

@Override
public int compare(File f1, File f2) {
return f1.getPath().compareToIgnoreCase(f2.getPath());
String s1 = f1.getPath();
String s2 = f2.getPath();
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
return c1 - c2;
}
}
}
return n1 - n2;
}

@Override
Expand Down
32 changes: 25 additions & 7 deletions test/jdk/java/io/File/CompareTo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,12 +22,15 @@
*/

/* @test
@bug 4131169
@summary Test respecified compareTo method
* @bug 4131169 8191963
* @summary Test compareTo and equals methods
* @library .. /test/lib
* @build jdk.test.lib.Platform
* @run main CompareTo
*/

import java.io.*;

import java.io.File;
import jdk.test.lib.Platform;

public class CompareTo {

Expand All @@ -36,6 +39,19 @@ private static void testWin32() throws Exception {
File f2 = new File("B");
if (!(f1.compareTo(f2) < 0))
throw new Exception("compareTo incorrect");

// U+0131 = ı 'LATIN SMALL LETTER DOTLESS I'
File smallDotlessI = new File("\u0131");
// U+0130 = İ 'LATIN CAPITAL LETTER I WITH DOT ABOVE'
File largeDotfullI = new File("\u0130");
File latinCapitalI = new File("I");

boolean shouldBeEqual= smallDotlessI.equals(latinCapitalI);
if (!shouldBeEqual)
throw new Exception("Small dotless \"i\" does not equal \"I\"");
boolean shouldNotBeEqual = largeDotfullI.equals(latinCapitalI);
if (shouldNotBeEqual)
throw new Exception("Large dotted \"I\" equals \"I\"");
}

private static void testUnix() throws Exception {
Expand All @@ -46,8 +62,10 @@ private static void testUnix() throws Exception {
}

public static void main(String[] args) throws Exception {
if (File.separatorChar == '\\') testWin32();
if (File.separatorChar == '/') testUnix();
if (Platform.isWindows())
testWin32();
else
testUnix();
}

}