db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1559946 - /db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
Date Tue, 21 Jan 2014 08:59:09 GMT
Author: kahatlen
Date: Tue Jan 21 08:59:08 2014
New Revision: 1559946

URL: http://svn.apache.org/r1559946
Log:
DERBY-6455: Infinite loop in NetworkServerControlImpl.ensureDataInBuffer

Make NetworkServerControlImpl.ensureDataInBuffer() stop immediately if
it reaches end-of-stream.

Based on patch contributed by Martin Janda.

Modified:
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java?rev=1559946&r1=1559945&r2=1559946&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
(original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
Tue Jan 21 08:59:08 2014
@@ -2893,21 +2893,33 @@ public final class NetworkServerControlI
     
 
     /**
-     * Ensure the reply buffer is at large enought to hold all the data;
+     * Ensure the reply buffer is large enough to hold all the data;
      * don't just rely on OS level defaults
      *
      *
      * @param minimumBytesNeeded    size of buffer required
+     * @param failOnEOS tells whether or not an error should be raised if
+     * end-of-stream is reached before the requested amount of bytes could
+     * be read
      * @exception Exception throws an exception if a problem reading the reply
      */
-    private void ensureDataInBuffer(int minimumBytesNeeded) throws Exception
+    private void ensureDataInBuffer(int minimumBytesNeeded, boolean failOnEOS)
+            throws Exception
     {
         // make sure the buffer is large enough
         while ((replyBufferCount - replyBufferPos) < minimumBytesNeeded)
         {
             try {
                 int bytesRead = clientIs.read(replyBuffer, replyBufferCount, replyBuffer.length
- replyBufferCount);
-                replyBufferCount += bytesRead;
+                if (bytesRead == -1) {
+                    if (failOnEOS) {
+                        consolePropertyMessage(
+                                "DRDA_InvalidReplyTooShort.S", true);
+                    }
+                    break;
+                } else {
+                    replyBufferCount += bytesRead;
+                }
         
             } catch (IOException e)
             {
@@ -2946,7 +2958,7 @@ public final class NetworkServerControlI
      */
     private void readCommandReplyHeader() throws Exception
     {
-        ensureDataInBuffer(REPLY_HEADER_LENGTH);
+        ensureDataInBuffer(REPLY_HEADER_LENGTH, false);
         if (replyBufferCount < REPLY_HEADER_LENGTH)
         {
             consolePropertyMessage("DRDA_InvalidReplyHeader1.S", Integer.toString(replyBufferCount));
@@ -2964,9 +2976,7 @@ public final class NetworkServerControlI
      */
     private int readShort() throws Exception
     {
-        ensureDataInBuffer(2);
-        if (replyBufferPos + 2 > replyBufferCount)
-            consolePropertyMessage("DRDA_InvalidReplyTooShort.S", true);
+        ensureDataInBuffer(2, true);
         return ((replyBuffer[replyBufferPos++] & 0xff) << 8) +
                 (replyBuffer[replyBufferPos++] & 0xff);
     }
@@ -2976,9 +2986,7 @@ public final class NetworkServerControlI
      */
     private int readInt() throws Exception
     {
-        ensureDataInBuffer(4);
-        if (replyBufferPos + 4 > replyBufferCount)
-            consolePropertyMessage("DRDA_InvalidReplyTooShort.S", true);
+        ensureDataInBuffer(4, true);
         return ((replyBuffer[replyBufferPos++] & 0xff) << 24) +
             ((replyBuffer[replyBufferPos++] & 0xff) << 16) +
             ((replyBuffer[replyBufferPos++] & 0xff) << 8) +
@@ -3015,9 +3023,7 @@ public final class NetworkServerControlI
     private String readLDString() throws Exception
     {
         int strlen = readShort();
-        ensureDataInBuffer(strlen);
-        if (replyBufferPos + strlen > replyBufferCount)
-            consolePropertyMessage("DRDA_InvalidReplyTooShort.S", true);
+        ensureDataInBuffer(strlen, true);
         String retval= new String(replyBuffer, replyBufferPos, strlen, DEFAULT_ENCODING);
         replyBufferPos += strlen;
         return retval;
@@ -3049,9 +3055,7 @@ public final class NetworkServerControlI
     private byte[] readLDBytes() throws Exception
     {
         int len = readShort();
-        ensureDataInBuffer(len);
-        if (replyBufferPos + len > replyBufferCount)
-            consolePropertyMessage("DRDA_InvalidReplyTooShort.S", true);
+        ensureDataInBuffer(len, true);
         byte [] retval =  new byte[len];
         for (int i = 0; i < len; i++)
             retval[i] = replyBuffer[replyBufferPos++];



Mime
View raw message