GithubHelp home page GithubHelp logo

Comments (3)

GoogleCodeExporter avatar GoogleCodeExporter commented on July 24, 2024
I think I figured it out.  In StringSerializer there is a bug when calculating 
the size of a new buffer when there isn't enough room in the current buffer for 
the varint size of the string.  The existing code seems to assume that the 
varint will only be 1 byte instead of using the expectedSize which breaks when 
it takes more than 1 byte for the length of the string and the length of the 
string is larger than the session.nextBufferSize.

Patch with Tests and attempted Fix attached and pasted below:

Index: 
protostuff-api/src/test/java/com/dyuproject/protostuff/StringSerializerTest.java
===================================================================
--- 
protostuff-api/src/test/java/com/dyuproject/protostuff/StringSerializerTest.java
    (revision 1012)
+++ 
protostuff-api/src/test/java/com/dyuproject/protostuff/StringSerializerTest.java
    (working copy)
@@ -145,7 +145,73 @@
         Double.MAX_VALUE,
         Double.MIN_VALUE
     };
-    
+
+    public void testVarDelimitedBoundryTwoByte() throws Exception
+    {
+        int size = 0x80+16; // takes 2 bytes for size
+
+        checkVarDelimitedBoundry(1, size);
+        checkVarDelimitedBoundry(2, size);
+        checkVarDelimitedBoundry(3, size);
+    }
+
+    public void testVarDelimitedBoundryThreeByte() throws Exception
+    {
+        int size = 0x800+16; // takes 3 bytes for size
+
+        checkVarDelimitedBoundry(1, size);
+        checkVarDelimitedBoundry(2, size);
+        checkVarDelimitedBoundry(3, size);
+        checkVarDelimitedBoundry(4, size);
+    }
+
+    public void testVarDelimitedBoundryFourByte() throws Exception
+    {
+        int size = 0x8000+16; // takes 4 bytes for size
+
+        checkVarDelimitedBoundry(1, size);
+        checkVarDelimitedBoundry(2, size);
+        checkVarDelimitedBoundry(3, size);
+        checkVarDelimitedBoundry(4, size);
+        checkVarDelimitedBoundry(5, size);
+    }
+
+    public void testVarDelimitedBoundryFiveByte() throws Exception
+    {
+        int size = 0x80000+16; // takes 5 bytes for size
+
+        checkVarDelimitedBoundry(1, size);
+        checkVarDelimitedBoundry(2, size);
+        checkVarDelimitedBoundry(3, size);
+        checkVarDelimitedBoundry(4, size);
+        checkVarDelimitedBoundry(5, size);
+        checkVarDelimitedBoundry(6, size);
+    }
+
+    public String repeatChar(char ch, int times)
+    {
+        StringBuilder sb = new StringBuilder(times);
+        for(int i = 0; i < times; i++)
+        {
+            sb.append(ch);
+        }
+        return sb.toString();
+    }
+
+    public void checkVarDelimitedBoundry(int initialGap, int secondWriteSize)
+    {
+        int bufferSize = 256;
+        final LinkedBuffer lb = LinkedBuffer.allocate(bufferSize);
+        WriteSession session = new WriteSession(lb, bufferSize);
+
+        // Should fill up the buffer with 1 byte left
+        StringSerializer.writeUTF8(repeatChar('a', bufferSize-initialGap), 
session, lb);
+
+        // Write a string that requires 3 bytes for the length and is larger
+        // than the next buffer size
+        StringSerializer.writeUTF8VarDelimited(repeatChar('a', 
secondWriteSize), session, lb);
+    }
+
     public void testInt() throws Exception
     {
         for(int i : int_targets)
Index: 
protostuff-api/src/main/java/com/dyuproject/protostuff/StringSerializer.java
===================================================================
--- 
protostuff-api/src/main/java/com/dyuproject/protostuff/StringSerializer.java    
    (revision 1012)
+++ 
protostuff-api/src/main/java/com/dyuproject/protostuff/StringSerializer.java    
    (working copy)
@@ -732,7 +732,7 @@
         {
             // not enough space for the varint.
             // create a new buffer.
-            lb = new LinkedBuffer(len+1 > session.nextBufferSize ? len+1 : 
session.nextBufferSize, 
+            lb = new LinkedBuffer(len+expectedSize > session.nextBufferSize ? 
len+expectedSize : session.nextBufferSize,
                     lb);
             offset = 0;
             lb.offset = expectedSize;

Original comment by [email protected] on 10 Sep 2010 at 10:05

Attachments:

from protostuff-googlecode-exported.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 24, 2024
Attached an updated patch to fix a test case (it wasn't properly testing for 
the condition) and fix some of the comments.

Original comment by [email protected] on 10 Sep 2010 at 10:14

Attachments:

from protostuff-googlecode-exported.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 24, 2024
Your patch has been applied. (rev 1017)
Thanks Tim!

Original comment by [email protected] on 11 Sep 2010 at 6:24

  • Changed state: Fixed

from protostuff-googlecode-exported.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.