7146776: deadlock between URLStreamHandler.getHostAddress and file.Handler.openconnection

Reviewed-by: alanb, chegar
This commit is contained in:
Jaikiran Pai 2021-01-18 11:53:22 +00:00
parent 61292be755
commit db9c114d40
2 changed files with 30 additions and 20 deletions
src/java.base/share/classes/java/net

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2021, 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
@ -249,7 +249,7 @@ public final class URL implements java.io.Serializable {
* The host's IP address, used in equals and hashCode.
* Computed on demand. An uninitialized or unknown hostAddress is null.
*/
transient InetAddress hostAddress;
private transient InetAddress hostAddress;
/**
* The URLStreamHandler for this URL.
@ -809,6 +809,31 @@ public final class URL implements java.io.Serializable {
}
}
/**
* Returns the address of the host represented by this URL.
* A {@link SecurityException} or an {@link UnknownHostException}
* while getting the host address will result in this method returning
* {@code null}
*
* @return an {@link InetAddress} representing the host
*/
synchronized InetAddress getHostAddress() {
if (hostAddress != null) {
return hostAddress;
}
if (host == null || host.isEmpty()) {
return null;
}
try {
hostAddress = InetAddress.getByName(host);
} catch (UnknownHostException | SecurityException ex) {
return null;
}
return hostAddress;
}
/**
* Gets the query part of this {@code URL}.
*

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2021, 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
@ -443,23 +443,8 @@ public abstract class URLStreamHandler {
* IP address.
* @since 1.3
*/
protected synchronized InetAddress getHostAddress(URL u) {
if (u.hostAddress != null)
return u.hostAddress;
String host = u.getHost();
if (host == null || host.isEmpty()) {
return null;
} else {
try {
u.hostAddress = InetAddress.getByName(host);
} catch (UnknownHostException ex) {
return null;
} catch (SecurityException se) {
return null;
}
}
return u.hostAddress;
protected InetAddress getHostAddress(URL u) {
return u.getHostAddress();
}
/**