This is the mail archive of the mailing list for the Java project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[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 <>

	* gnu/java/net/ (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/
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/,v
retrieving revision 1.11
diff -c -p -r1.11
*** gnu/java/net/	1 Feb 2005 19:22:47 -0000	1.11
--- gnu/java/net/	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)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]