CMertens / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Serializing a 0 length byte array fails on next field. #218

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============

What steps will reproduce the problem?
1. Create a class to serialize like

    [ProtoContract]    
    public class Test
    {
        [ProtoMember(1)]
        public byte[] BackgroundImageToUpload { get; set; }

        [ProtoMember(2)]
        public string Title { get; set; }

    }

2. Call code like:

      RuntimeTypeModel typeModel = TypeModel.Create();
      typeModel.AutoAddMissingTypes = true;

     FileStream fileStream = new FileStream(@"c:\file.test", FileMode.Create);

     typeModel.Serialize(fileStream, new Test() { Title = "MyTitle"});

What is the expected output? What do you see instead?

Expecting that the instance will get serialized.  Instead, I get an exception 
   InvalidOperationException - Cannot write a String header until the String data has been written.

What version of the product are you using? On what operating system?

v2 - beta release on Windows Phone 7

Please provide any additional information below.

I've fixed the bug in my dev environment by changing in ProtoWriter.cs within 
the WriteBytes() static method, the following:

   if (length == 0) return;

To 

   if (length == 0)
   {
      writer.wireType = WireType.None;
      return;
   }

When the length of the byte array is 0, the ProtoWriter wasn't getting reset.

Original issue reported on code.google.com by david.ma...@hotmail.com on 9 Aug 2011 at 11:48

GoogleCodeExporter commented 9 years ago
You are quite correct - a stupid bug by me.

(not that it matters, but I moved the wire-type reset *higher* instead)

Original comment by marc.gravell on 10 Aug 2011 at 6:04

GoogleCodeExporter commented 9 years ago

Original comment by marc.gravell on 31 Aug 2012 at 3:48