Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
7146776: deadlock between URLStreamHandler.getHostAddress and file.Ha…
Browse files Browse the repository at this point in the history
…ndler.openconnection

Backport-of: db9c114d40cd20c2854121ed8b40a7c9ef8e59b3
  • Loading branch information
Evan Whelan authored and coffeys committed Jan 25, 2021
1 parent e9dae4d commit d3ecad7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
29 changes: 27 additions & 2 deletions src/java.base/share/classes/java/net/URL.java
@@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -842,6 +842,31 @@ void set(String protocol, String host, int port,
}
}

/**
* 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}.
*
Expand Down
21 changes: 3 additions & 18 deletions src/java.base/share/classes/java/net/URLStreamHandler.java
@@ -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
Expand Down Expand Up @@ -443,23 +443,8 @@ protected boolean sameFile(URL u1, URL u2) {
* 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();
}

/**
Expand Down

1 comment on commit d3ecad7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.