sun.net.www.protocol.https.HttpsURLConnectionImpl doesn't equal to self
Stanimir Simeonoff
stanimir at riflexo.com
Mon Aug 18 14:04:36 UTC 2014
Hi,
As the title says there is a major bug with HttpsURLConnection as it breaks
the contract of equals. So the instance cannot be contained (mostly
removed) in any collection reliably. (Concurrent)HashMap has a provision to
test equality by reference prior calling equals, though.
Finding the bug in production is quite a ride as the http variant doesn't
exhibit the problem.
Here is a simple test case.
public static void main(String[] args) throws Throwable{
URLConnection c = new URL("https://oracle.com").openConnection();
System.out.println(c.getClass().getName()+" equals self: "
+c.equals(c));
c.getInputStream().close();
System.out.println(c.getClass().getName()+" equals self: "
+c.equals(c));
}
The culprit is in HttpsURLConnectionImpl.equals that blindly calls
delagate.equals:
public boolean equals(Object obj) {
return delegate.equals(obj);
}
It should be changed to:
public boolean equals(Object obj) {
return this==obj || (obj instanceof HttpsURLConnectionImpl) &&
delegate.equals( ((HttpsURLConnectionImpl)obj).delegate );
}
The class has some other issue that involves declaring "finalize" method to
simply call delegate's dispose. The finalize method is unneeded and just
creates unnecessary link in the finalization queue + Finalizer object.
Thanks
Stanimir
More information about the core-libs-dev
mailing list