cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sn...@apache.org
Subject [1/3] cassandra git commit: UnbufferedDataOutputstreamPlus.writeUTF fails for 0 length and > 8190 length strings
Date Wed, 26 Aug 2015 22:59:19 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 7ab241e96 -> 47886dff3


UnbufferedDataOutputstreamPlus.writeUTF fails for 0 length and > 8190 length strings

patch by Ariel Weisberg; reviewed by Robert Stupp for CASSANDRA-10203


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/27bc7a52
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/27bc7a52
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/27bc7a52

Branch: refs/heads/trunk
Commit: 27bc7a5202761bcb8a12f7f7c7fba65ae1a19934
Parents: bc25ca1
Author: Ariel Weisberg <ariel.weisberg@datastax.com>
Authored: Thu Aug 27 00:52:03 2015 +0200
Committer: Robert Stupp <snazy@snazy.de>
Committed: Thu Aug 27 00:52:03 2015 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../io/util/UnbufferedDataOutputStreamPlus.java | 10 ++++-
 .../io/util/BufferedDataOutputStreamTest.java   | 44 ++++++++++++++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/27bc7a52/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index f9d9ae1..ea11489 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.1
+ * Fix broken UnbufferedDataOutputStreamPlus.writeUTF (CASSANDRA-10203)
  * (cqlsh) add CLEAR command (CASSANDRA-10086)
  * Support string literals as Role names for compatibility (CASSANDRA-10135)
  * Allow count(*) and count(1) to be use as normal aggregation (CASSANDRA-10114)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/27bc7a52/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java b/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
index d8036ff..10aefa6 100644
--- a/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
+++ b/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
@@ -23,7 +23,6 @@ import java.io.UTFDataFormatException;
 import java.nio.ByteBuffer;
 import java.nio.channels.WritableByteChannel;
 
-import org.apache.cassandra.config.Config;
 import org.apache.cassandra.utils.memory.MemoryUtil;
 
 import com.google.common.base.Function;
@@ -38,6 +37,8 @@ import com.google.common.base.Function;
  */
 public abstract class UnbufferedDataOutputStreamPlus extends DataOutputStreamPlus
 {
+    private static final byte[] zeroBytes = new byte[2];
+
     protected UnbufferedDataOutputStreamPlus()
     {
         super();
@@ -253,6 +254,12 @@ public abstract class UnbufferedDataOutputStreamPlus extends DataOutputStreamPlu
     public static void writeUTF(String str, DataOutput out) throws IOException
     {
         int length = str.length();
+        if (length == 0)
+        {
+            out.write(zeroBytes);
+            return;
+        }
+
         int utfCount = 0;
         int maxSize = 2;
         for (int i = 0 ; i < length ; i++)
@@ -284,7 +291,6 @@ public abstract class UnbufferedDataOutputStreamPlus extends DataOutputStreamPlu
                 for (int i = firstIndex ; i < runLength; i++)
                     utfBytes[i] = (byte) str.charAt(offset + i);
                 out.write(utfBytes, 0, runLength);
-                offset += firstIndex;
                 firstIndex = 0;
             }
         }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/27bc7a52/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java b/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java
index d0819fe..ffe9cb9 100644
--- a/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java
+++ b/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java
@@ -1,6 +1,11 @@
 package org.apache.cassandra.io.util;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.UTFDataFormatException;
@@ -442,4 +447,43 @@ public class BufferedDataOutputStreamTest
         }
         return count;
     }
+
+    @Test
+    public void testWriteUTF() throws Exception
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        DataOutput dataOut = new DataOutputStream(baos);
+
+        StringBuilder sb = new StringBuilder(65535);
+        for (int ii = 0; ii < 1 << 16; ii++)
+        {
+            String s = sb.toString();
+            UnbufferedDataOutputStreamPlus.writeUTF(s, dataOut);
+            DataInput dataIn = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
+            assertEquals(s, dataIn.readUTF());
+            baos.reset();
+            sb.append("a");
+        }
+    }
+
+    @Test
+    public void testWriteUTFBigChar() throws Exception
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        DataOutput dataOut = new DataOutputStream(baos);
+
+        StringBuilder sb = new StringBuilder(65535);
+        for (int ii = 0; ii < 1 << 15; ii++)
+        {
+            String s = sb.toString();
+            UnbufferedDataOutputStreamPlus.writeUTF(s, dataOut);
+            DataInput dataIn = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
+            assertEquals(s, dataIn.readUTF());
+            baos.reset();
+            if (ii == (1 << 15) - 1)
+                sb.append("a");
+            else
+                sb.append(twoByte);
+        }
+    }
 }


Mime
View raw message