String length serialization bug
TechInterMezzo opened this issue · comments
There is a bug in the string serialization code. For strings longer than 254 bytes you write a marker with a value of 254. Shouldn't it be 255? Also you add 1 to the length before writing it to the stream and subtract 1 after reading the length from the stream (source). I don't know why you did this in the first place, but this also collides with the marker and produces some bugs for me.
You are correct.
I have fixed this in the dotnet core dev branch:
https://github.com/rogeralsing/Wire/blob/dotnetcore/Wire/ValueSerializers/StringSerializer.cs
Regarding the +1 -1 length.
0 = null
1 = 0 length
2 = 1 length
etc..
255 = marker for long string
I have a test that verifies length per length
for (int i = 0; i < 2000; i++)
{
var str = new string('a',i);
SerializeAndAssert(str);
}
The updated code now passes this test.
Thank you for the fix and explanation. Your serializer is really useful for my simple ipc library.