8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
Reviewed-by: alanb, dfuchs
--- a/src/java.base/share/classes/java/net/ServerSocket.java Tue Mar 26 09:24:01 2019 -0700
+++ b/src/java.base/share/classes/java/net/ServerSocket.java Tue Mar 26 17:02:11 2019 +0000
@@ -749,14 +749,17 @@
* timeout must be {@code > 0}.
* A timeout of zero is interpreted as an infinite timeout.
* @param timeout the specified timeout, in milliseconds
- * @exception SocketException if there is an error in
- * the underlying protocol, such as a TCP error.
+ * @throws SocketException if there is an error in the underlying protocol,
+ * such as a TCP error
+ * @throws IllegalArgumentException if {@code timeout} is negative
* @since 1.1
* @see #getSoTimeout()
*/
public synchronized void setSoTimeout(int timeout) throws SocketException {
if (isClosed())
throw new SocketException("Socket is closed");
+ if (timeout < 0)
+ throw new IllegalArgumentException("timeout < 0");
getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout);
}
--- a/src/java.base/share/classes/java/net/Socket.java Tue Mar 26 09:24:01 2019 -0700
+++ b/src/java.base/share/classes/java/net/Socket.java Tue Mar 26 17:02:11 2019 +0000
@@ -581,7 +581,8 @@
* if this socket has an associated channel,
* and the channel is in non-blocking mode
* @throws IllegalArgumentException if endpoint is null or is a
- * SocketAddress subclass not supported by this socket
+ * SocketAddress subclass not supported by this socket, or
+ * if {@code timeout} is negative
* @since 1.4
* @spec JSR-51
*/
@@ -1212,8 +1213,9 @@
* A timeout of zero is interpreted as an infinite timeout.
*
* @param timeout the specified timeout, in milliseconds.
- * @exception SocketException if there is an error
- * in the underlying protocol, such as a TCP error.
+ * @throws SocketException if there is an error in the underlying protocol,
+ * such as a TCP error
+ * @throws IllegalArgumentException if {@code timeout} is negative
* @since 1.1
* @see #getSoTimeout()
*/
--- a/test/jdk/java/net/Socket/Timeouts.java Tue Mar 26 09:24:01 2019 -0700
+++ b/test/jdk/java/net/Socket/Timeouts.java Tue Mar 26 17:02:11 2019 +0000
@@ -85,8 +85,7 @@
}
/**
- * Test connect with a negative timeout. This case is not currently specified
- * but the long standing behavior is to throw IllegalArgumentException.
+ * Test connect with a negative timeout.
*/
public void testTimedConnect4() throws IOException {
try (ServerSocket ss = new ServerSocket(0)) {
@@ -393,8 +392,7 @@
}
/**
- * Test Socket setSoTimeout with a negative timeout. This case is not currently
- * specified but the long standing behavior is to throw IllegalArgumentException.
+ * Test Socket setSoTimeout with a negative timeout.
*/
@Test(expectedExceptions = { IllegalArgumentException.class })
public void testBadTimeout1() throws IOException {
@@ -404,9 +402,7 @@
}
/**
- * Test ServerSocket setSoTimeout with a negative timeout. This case is not
- * currently specified but the long standing behavior is to throw
- * IllegalArgumentException.
+ * Test ServerSocket setSoTimeout with a negative timeout.
*/
@Test(expectedExceptions = { IllegalArgumentException.class })
public void testBadTimeout2() throws IOException {