This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
[Patch] Fix Socket read returns EOF when count == 0...
- From: David Daney <ddaney at avtrex dot com>
- To: Java Patch List <java-patches at gcc dot gnu dot org>
- Date: Mon, 14 Mar 2005 15:57:35 -0800
- Subject: [Patch] Fix Socket read returns EOF when count == 0...
For POSIX, a call to read(byte[] b, int off, int count) with count of
zero on a Socket's InputStream returns -1 signaling EOF.
This is because the count is propagated the the ::recv() system call
which returns the number of bytes read or zero to signal EOF. Since
zero bytes were requested we report EOF when we should report zero bytes
transfered.
Before recent fixes to BufferedInputStream, some HTTP URLConnections
were being broken because of this behavior. Since the
BufferedInputStream was fixed I have not experienced any problems, but
this is a latent bug waiting to bite someone else.
FWIW: FileInputStream has similar handling for the count == 0 case.
Tested on the HEAD on i686-pc-linux with make check in libjava with no
regressions. No regressions in mauve in the 4.0 branch (HEAD not tested
in mauve due to ICE in mauve)
2005-03-14 David Daney <ddaney@avtrex.com>
* gnu/java/net/natPlainSocketImplPosix.cc (read_helper): Handle
count == 0 case.
O.K to commit to HEAD?
How about the 4.0 branch for good measure?
David Daney.
Index: gnu/java/net/natPlainSocketImplPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/natPlainSocketImplPosix.cc,v
retrieving revision 1.11
diff -c -p -r1.11 natPlainSocketImplPosix.cc
*** gnu/java/net/natPlainSocketImplPosix.cc 1 Feb 2005 19:22:47 -0000 1.11
--- gnu/java/net/natPlainSocketImplPosix.cc 14 Mar 2005 23:12:23 -0000
***************
*** 1,4 ****
! /* Copyright (C) 2003, 2004 Free Software Foundation
This file is part of libgcj.
--- 1,4 ----
! /* Copyright (C) 2003, 2004, 2005 Free Software Foundation
This file is part of libgcj.
*************** gnu::java::net::PlainSocketImpl$SocketIn
*** 405,410 ****
--- 405,415 ----
static jint
read_helper (jint native_fd, jint timeout, jbyte *bytes, jint count)
{
+ // If zero bytes were requested, short circuit so that recv
+ // doesn't signal EOF.
+ if (count == 0)
+ return 0;
+
// Do timeouts via select.
if (timeout > 0 && native_fd >= 0 && native_fd < FD_SETSIZE)
{