Comments (3)
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.
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.
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)
- Would like support for repeated accessors with out "List" appeneded
- Enter one-line summary HOT 2
- getSchema() of java.util.Properties fails HOT 2
- optimize xml format
- Enum alias support
- YamlOutput bug on repeated message fields HOT 1
- Deserializing a message which is protostuff-runtime serialized and has custom class as field HOT 3
- Visual disturbance of @Deprecated annotation HOT 12
- Support for newing object instances on Android 4.3+ devices HOT 2
- ConcurrentLinkedDeque support HOT 1
- Produces uncompilable Java when a field is called "default" HOT 4
- 1.0.7 Build error: ProtoParserTest fails HOT 2
- protostuff-parser and protostuff-compiler disagree on required version of antlr
- Optimize ProtobufOuput for small nested messages (max size of 127) HOT 1
- How can I serialize a negative int32 to the protobuf format using zigzag encoding? HOT 2
- How to Ignore Array of Null Values in JSON
- enums_by_name does not seem to be working using maven plugin
- The value of DataInput.readByte() should not be eof-checked since that method throws EOFException HOT 1
- Wrong output path is generated in Windows for .java files HOT 3
- Unable to parse proto enum containing hex constants HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protostuff-googlecode-exported.