This is the mail archive of the java-patches@gcc.gnu.org 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]

FYI: Patch: java.net.DatagramPacket


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi list,


I commited the attached patch to fix a bug found by Norbert Frese and 
reported to java@gcc.gnu.org.


Michael
- -- 
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/zyNIWSOgCCdjSDsRAveNAJwJSiEAHc/c2er4n5XW+zF2vmiUOACdE6di
bKgv3ozQ921uzH95OkeJk5Y=
=wvd1
-----END PGP SIGNATURE-----
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2415
diff -u -b -B -r1.2415 ChangeLog
--- ChangeLog	4 Dec 2003 00:31:26 -0000	1.2415
+++ ChangeLog	4 Dec 2003 10:42:04 -0000
@@ -1,3 +1,20 @@
+2003-12-04  Michael Koch  <konqueror@gmx.de>
+
+	* java/net/DatagramPacket.java
+	(length): Made packge-private to make it accessible via CNI.
+	(maxlen): New field.
+	(DatagramPacket): Cleaned up.
+	(setSocketAddress): Add message to exception.
+	(setData): Call other setData().
+	(setData): Call setLength().
+	(setLength): Initialize maxlen too.
+	* gnu/java/net/natPlainDatagramSocketImplPosix.cc (peekData):
+	Get maximal length from maxlen field, set length field directly.
+	(receive): Likewise.
+	* gnu/java/net/natPlainDatagramSocketImplWin32.cc (peekData):
+	Get maximal length from maxlen field, set length field directly.
+	(receive): Likewise.
+
 2003-12-03  Mohan Embar  <gnustuff@thisiscool.com>
 
 	* gnu/java/nio/natSelectorImplPosix.cc
Index: java/net/DatagramPacket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/DatagramPacket.java,v
retrieving revision 1.13
diff -u -b -B -r1.13 DatagramPacket.java
--- java/net/DatagramPacket.java	26 Nov 2003 10:11:45 -0000	1.13
+++ java/net/DatagramPacket.java	4 Dec 2003 10:42:04 -0000
@@ -78,13 +78,18 @@
   private int offset;
 
   /**
-   * The length of the data buffer to send
+   * The length of the data buffer to send.
    */
-  private int length;
+  int length;
+
+  /**
+   * The maximal length of the buffer.
+   */
+  int maxlen;
 
   /**
    * The address to which the packet should be sent or from which it
-   * was received
+   * was received.
    */
   private InetAddress address;
 
@@ -106,21 +111,9 @@
    */
   public DatagramPacket(byte[] buf, int offset, int length)
   {
-    if (buf == null)
-      throw new NullPointerException("Null buffer");
-    if (offset < 0)
-      throw new IllegalArgumentException("Invalid offset: " + offset);
-    if (length < 0)
-      throw new IllegalArgumentException("Invalid length: " + length);
-    if (offset + length > buf.length)
-      throw new IllegalArgumentException("Potential buffer overflow - offset: "
-			+ offset + " length: " + length);
-
-    buffer = buf;
-    this.offset = offset;
-    this.length = length;
-    this.address = null;
-    this.port = -1;
+    setData(buf, offset, length);
+    address = null;
+    port = -1;
   }
 
   /**
@@ -150,25 +143,9 @@
   public DatagramPacket(byte[] buf, int offset, int length,
 	InetAddress address, int port)
   {
-    if (buf == null)
-      throw new NullPointerException("Null buffer");
-    if (offset < 0)
-      throw new IllegalArgumentException("Invalid offset: " + offset);
-    if (length < 0)
-      throw new IllegalArgumentException("Invalid length: " + length);
-    if (offset + length > buf.length)
-      throw new IllegalArgumentException("Potential buffer overflow - offset: "
-			+ offset + " length: " + length);
-    if (port < 0 || port > 65535)
-      throw new IllegalArgumentException("Invalid port: " + port);
-    if (address == null)
-      throw new NullPointerException("Null address");
-
-    buffer = buf;
-    this.offset = offset;
-    this.length = length;
-    this.address = address;
-    this.port = port;
+    setData(buf, offset, length);
+    setAddress(address);
+    setPort(port);
   }
 
   /**
@@ -203,8 +180,13 @@
 		        SocketAddress address)
      throws SocketException
   {
-    this(buf, offset, length, ((InetSocketAddress)address).getAddress(),
-         ((InetSocketAddress)address).getPort());
+    if (! (address instanceof InetSocketAddress))
+      throw new IllegalArgumentException("unsupported address type");
+
+    InetSocketAddress tmp = (InetSocketAddress) address;
+    setData(buf, offset, length);
+    setAddress(tmp.getAddress());
+    setPort(tmp.getPort());
   }
 
   /**
@@ -223,8 +205,7 @@
   public DatagramPacket(byte[] buf, int length, SocketAddress address)
     throws SocketException
   {
-    this(buf, 0, length, ((InetSocketAddress)address).getAddress(),
-         ((InetSocketAddress)address).getPort());
+    this(buf, 0, length, address);
   }
 
   /**
@@ -330,9 +311,10 @@
   public void setSocketAddress(SocketAddress address)
     throws IllegalArgumentException
   {
-    if (address == null) throw new IllegalArgumentException();
+    if (address == null)
+      throw new IllegalArgumentException("address may not be null");
 
-    InetSocketAddress tmp = (InetSocketAddress)address;
+    InetSocketAddress tmp = (InetSocketAddress) address;
     this.address = tmp.getAddress();
     this.port = tmp.getPort();
   }
@@ -359,14 +341,9 @@
    *
    * @since 1.1
    */
-  public synchronized void setData(byte[] buf)
+  public void setData(byte[] buf)
   {
-    // This form of setData requires setLength to be called separately
-    // and subsequently.
-    if (buf == null)
-      throw new NullPointerException("Null buffer");
-
-    buffer = buf;
+    setData(buf, 0, buf.length);
   }
 
   /**
@@ -388,15 +365,10 @@
       throw new NullPointerException("Null buffer");
     if (offset < 0)
       throw new IllegalArgumentException("Invalid offset: " + offset);
-    if (length < 0)
-      throw new IllegalArgumentException("Invalid length: " + length);
-    if (offset + length > buf.length)
-      throw new IllegalArgumentException("Potential buffer overflow - offset: "
-			+ offset + " length: " + length);
 
     buffer = buf;
     this.offset = offset;
-    this.length = length;
+    setLength(length);
   }
 
   /**
@@ -418,6 +390,6 @@
 			+ offset + " length: " + length);
 
     this.length = length;
+    this.maxlen = length;
   }
-} // class DatagramPacket
-
+}
Index: gnu/java/net/natPlainDatagramSocketImplPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc,v
retrieving revision 1.4
diff -u -b -B -r1.4 natPlainDatagramSocketImplPosix.cc
--- gnu/java/net/natPlainDatagramSocketImplPosix.cc	22 Nov 2003 16:49:47 -0000	1.4
+++ gnu/java/net/natPlainDatagramSocketImplPosix.cc	4 Dec 2003 10:42:04 -0000
@@ -209,7 +209,7 @@
   union SockAddr u;
   socklen_t addrlen = sizeof(u);
   jbyte *dbytes = elements (p->getData()) + p->getOffset();
-  jint maxlen = p->getData()->length - p->getOffset();
+  jint maxlen = p->maxlen - p->getOffset();
   ssize_t retlen = 0;
 
   // Do timeouts via select since SO_RCVTIMEO is not always available.
@@ -255,7 +255,7 @@
 
   p->setAddress (new ::java::net::InetAddress (raddr, NULL));
   p->setPort (rport);
-  p->setLength ((jint) retlen);
+  p->length = (int) retlen;
   return rport;
 
  error:
@@ -329,7 +329,7 @@
   union SockAddr u;
   socklen_t addrlen = sizeof(u);
   jbyte *dbytes = elements (p->getData()) + p->getOffset();
-  jint maxlen = p->getData()->length - p->getOffset();
+  jint maxlen = p->maxlen - p->getOffset();
   ssize_t retlen = 0;
 
   // Do timeouts via select since SO_RCVTIMEO is not always available.
@@ -375,7 +375,7 @@
 
   p->setAddress (new ::java::net::InetAddress (raddr, NULL));
   p->setPort (rport);
-  p->setLength ((jint) retlen);
+  p->length = (jint) retlen;
   return;
 
  error:
Index: gnu/java/net/natPlainDatagramSocketImplWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/natPlainDatagramSocketImplWin32.cc,v
retrieving revision 1.5
diff -u -b -B -r1.5 natPlainDatagramSocketImplWin32.cc
--- gnu/java/net/natPlainDatagramSocketImplWin32.cc	23 Nov 2003 01:38:31 -0000	1.5
+++ gnu/java/net/natPlainDatagramSocketImplWin32.cc	4 Dec 2003 10:42:04 -0000
@@ -203,7 +203,7 @@
   union SockAddr u;
   socklen_t addrlen = sizeof(u);
   jbyte *dbytes = elements (p->getData()) + p->getOffset();
-  jint maxlen = p->getData()->length - p->getOffset();
+  jint maxlen = p->maxlen - p->getOffset();
   ssize_t retlen = 0;
 
   if (timeout > 0)
@@ -241,7 +241,7 @@
 
   p->setAddress (new ::java::net::InetAddress (raddr, NULL));
   p->setPort (rport);
-  p->setLength ((jint) retlen);
+  p->length = (jint) retlen;
   return rport;
 
 error:
@@ -318,7 +318,7 @@
   union SockAddr u;
   socklen_t addrlen = sizeof(u);
   jbyte *dbytes = elements (p->getData()) + p->getOffset();
-  jint maxlen = p->getData()->length - p->getOffset();
+  jint maxlen = p->maxlen - p->getOffset();
   ssize_t retlen = 0;
 
   if (timeout > 0)
@@ -359,7 +359,7 @@
 
   p->setAddress (new ::java::net::InetAddress (raddr, NULL));
   p->setPort (rport);
-  p->setLength ((jint) retlen);
+  p->length = (jint) retlen;
   return;
 
  error:

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