msgpack / msgpack-cli

MessagePack implementation for Common Language Infrastructure / msgpack.org[C#]
http://msgpack.org
Apache License 2.0
834 stars 175 forks source link

Possible BUG in SingleArrayBufferAllocator.cs #287

Open gviolator opened 6 years ago

gviolator commented 6 years ago
    private static byte[] Allocate( byte[] old, int requestSize )
        {
            if ( old.Length < 256 )
            {
                return new byte[ 256 ];
            }

            // Use golden ratio to improve linear memory range reusability (of LOH)
            var newSize = Math.Max( ( long )( old.Length * 1.1618 ), requestSize + ( long )old.Length );
            if ( newSize > Int32.MaxValue )
            {
                return null;
            }

            return new byte[ newSize ];
        }

May be instead of

if ( old.Length < 256 )

should be

if (requestSize  < 256 )

Because if requested buffer size is greater than 256 and current buffer's length is less than 256 (for example we just serialize big string), then we fail to allocate, and whole serialization process will fail ...

yfakariya commented 6 years ago

Thank you for reporting. I'll add test case for this, and then fix for 1.0.