jefffhaynes / BinarySerializer

A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
MIT License
289 stars 62 forks source link

[BUG] FieldBitLength use can cause hang and consume all memory #180

Closed knutsr closed 2 years ago

knutsr commented 2 years ago

Hi,

We ran into an issue using FieldBitLength. We have defined a class with some byte-wise fields and some bit-wise fields. Trying to deserialize an array of such class instance will fail if the input array size is not an exact multiple of the size of a single instance. The Deserialize call never returns and consumes all available memory.

Trying to deserialize by calling ser.Deserialize<BitFieldsAlone>(serialized); with this definition succeeds with any input array length:

    public class BitFieldsAlone
    {
        [FieldOrder(0)]
        [FieldBitLength(7)]
        public byte SevenBits;
        [FieldOrder(1)]
        [FieldBitLength(1)]
        public byte OneBit;
    }

Trying to deserialize by calling ser.Deserialize<BitFieldsPrecededByByte[]>(serialized); with this definition fails with input array lengths that are not multiples of 2:

    public class BitFieldsPrecededByByte
    {
        [FieldOrder(0)] 
        public byte First;
        [FieldOrder(1)]
        [FieldBitLength(7)]
        public byte SevenBits;
        [FieldOrder(2)]
        [FieldBitLength(1)]
        public byte OneBit;
    }

See attached project file and code to reproduce.

BinarySerializationMemoryIssue.csproj.txt Program.cs.txt

jefffhaynes commented 2 years ago

Fixed in 8.6.2.1. Thanks!

knutsr commented 2 years ago

Great! Thank you.

knutsr commented 2 years ago

@jefffhaynes Unless I'm doing something stupid, it still fails in 8.6.2.1.

jefffhaynes commented 2 years ago

In the same place?

knutsr commented 2 years ago

I cloned the repo and added the attached project file and code to the solution. The test you added runs fine, but the test program does not.

jefffhaynes commented 2 years ago

Sorry, it was me doing something stupid :)

jefffhaynes commented 2 years ago

Try 8.6.2.2

knutsr commented 2 years ago

All is good now. Thanks a lot for fixing this :+1: