From a495aeeee86a137f788ca04f812c669f45deee53 Mon Sep 17 00:00:00 2001 From: yfakariya Date: Wed, 30 Aug 2017 23:38:53 +0900 Subject: [PATCH 1/2] Update change log. --- CHANGES.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index 578671459..27b2240d6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -703,3 +703,6 @@ Release 0.9.0 2017-8-26 * Fix polymorphic serializer error in Unity. Release 1.0.0-beta1 T.B.D. + + NEW FEATURES + * .NET Standard 2.0 which supports serializer source code generation on .NET Core. Note that serializer assembly generation is not supported. From 32da0901e00c942aad9391653005d5d4e7959f73 Mon Sep 17 00:00:00 2001 From: yfakariya Date: Wed, 30 Aug 2017 23:39:47 +0900 Subject: [PATCH 2/2] Fix ByteArrayPacker throws IndexOutOfBoundException when the buffer remaining bytes is equal to packed scalar size. #252 --- CHANGES.txt | 8 +++++ .../MessagePackByteArrayPacker.Pack.cs | 32 +++++++++++-------- .../MessagePackByteArrayPacker.Pack.tt | 7 ++-- .../Serialization/RegressionTests.cs | 18 ++++++++++- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 27b2240d6..b569bf562 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -702,7 +702,15 @@ Release 0.9.0 2017-8-26 * Fix MessagePackSerializer.Capability does not work correctly in Unity. * Fix polymorphic serializer error in Unity. +Release 0.9.1 2017-8-30 + + BUG FIXES + * Fix ByteArrayPacker throws IndexOutOfBoundException when the buffer remaining bytes is equal to packed scalar size. #252 + Release 1.0.0-beta1 T.B.D. NEW FEATURES * .NET Standard 2.0 which supports serializer source code generation on .NET Core. Note that serializer assembly generation is not supported. + + BUG FIXES + * Fix ByteArrayPacker throws IndexOutOfBoundException when the buffer remaining bytes is equal to packed scalar size. #252 diff --git a/src/MsgPack/MessagePackByteArrayPacker.Pack.cs b/src/MsgPack/MessagePackByteArrayPacker.Pack.cs index ad7fd3c9e..83ce2a451 100644 --- a/src/MsgPack/MessagePackByteArrayPacker.Pack.cs +++ b/src/MsgPack/MessagePackByteArrayPacker.Pack.cs @@ -1,4 +1,4 @@ - + #region -- License Terms -- // // MessagePack for CLI @@ -981,9 +981,10 @@ private void WriteBytes( byte header, byte value ) var buffer = this._buffer; var offset = this._offset; var remains = buffer.Length - offset; - if ( remains < sizeof( byte ) && !this._allocator.TryAllocate( buffer, sizeof( byte ), out buffer ) ) + const int requiredSize = sizeof( byte ) + 1; + if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) ) { - this.ThrowEofException( sizeof( byte ) ); + this.ThrowEofException( requiredSize ); } buffer[ offset ] = header; @@ -1000,9 +1001,10 @@ private void WriteBytes( byte header, ushort value ) var buffer = this._buffer; var offset = this._offset; var remains = buffer.Length - offset; - if ( remains < sizeof( ushort ) && !this._allocator.TryAllocate( buffer, sizeof( ushort ), out buffer ) ) + const int requiredSize = sizeof( ushort ) + 1; + if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) ) { - this.ThrowEofException( sizeof( ushort ) ); + this.ThrowEofException( requiredSize ); } buffer[ offset ] = header; @@ -1020,9 +1022,10 @@ private void WriteBytes( byte header, uint value ) var buffer = this._buffer; var offset = this._offset; var remains = buffer.Length - offset; - if ( remains < sizeof( uint ) && !this._allocator.TryAllocate( buffer, sizeof( uint ), out buffer ) ) + const int requiredSize = sizeof( uint ) + 1; + if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) ) { - this.ThrowEofException( sizeof( uint ) ); + this.ThrowEofException( requiredSize ); } buffer[ offset ] = header; @@ -1042,9 +1045,10 @@ private void WriteBytes( byte header, ulong value ) var buffer = this._buffer; var offset = this._offset; var remains = buffer.Length - offset; - if ( remains < sizeof( ulong ) && !this._allocator.TryAllocate( buffer, sizeof( ulong ), out buffer ) ) + const int requiredSize = sizeof( ulong ) + 1; + if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) ) { - this.ThrowEofException( sizeof( ulong ) ); + this.ThrowEofException( requiredSize ); } buffer[ offset ] = header; @@ -1069,9 +1073,10 @@ private void WriteBytes( byte header, float value ) var buffer = this._buffer; var offset = this._offset; var remains = buffer.Length - offset; - if ( remains < sizeof( float ) && !this._allocator.TryAllocate( buffer, sizeof( float ), out buffer ) ) + const int requiredSize = sizeof( float ) + 1; + if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) ) { - this.ThrowEofException( sizeof( float ) ); + this.ThrowEofException( requiredSize ); } buffer[ offset ] = header; @@ -1092,9 +1097,10 @@ private void WriteBytes( byte header, double value ) var buffer = this._buffer; var offset = this._offset; var remains = buffer.Length - offset; - if ( remains < sizeof( double ) && !this._allocator.TryAllocate( buffer, sizeof( double ), out buffer ) ) + const int requiredSize = sizeof( double ) + 1; + if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) ) { - this.ThrowEofException( sizeof( double ) ); + this.ThrowEofException( requiredSize ); } buffer[ offset ] = header; diff --git a/src/MsgPack/MessagePackByteArrayPacker.Pack.tt b/src/MsgPack/MessagePackByteArrayPacker.Pack.tt index 1b94b12aa..4b02bc458 100644 --- a/src/MsgPack/MessagePackByteArrayPacker.Pack.tt +++ b/src/MsgPack/MessagePackByteArrayPacker.Pack.tt @@ -1,4 +1,4 @@ -<#@ template debug="true" hostSpecific="true" language="C#" #> +<#@ template debug="true" hostSpecific="true" language="C#" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Core.dll" #> <#@ import namespace="System" #> @@ -77,9 +77,10 @@ namespace MsgPack var buffer = this._buffer; var offset = this._offset; var remains = buffer.Length - offset; - if ( remains < sizeof( <#= type #> ) && !this._allocator.TryAllocate( buffer, sizeof( <#= type #> ), out buffer ) ) + const int requiredSize = sizeof( <#= type #> ) + 1; + if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) ) { - this.ThrowEofException( sizeof( <#= type #> ) ); + this.ThrowEofException( requiredSize ); } buffer[ offset ] = header; diff --git a/test/MsgPack.UnitTest/Serialization/RegressionTests.cs b/test/MsgPack.UnitTest/Serialization/RegressionTests.cs index 10187df0a..6d0bb3f74 100644 --- a/test/MsgPack.UnitTest/Serialization/RegressionTests.cs +++ b/test/MsgPack.UnitTest/Serialization/RegressionTests.cs @@ -1,4 +1,4 @@ -#region -- License Terms -- +#region -- License Terms -- // // MessagePack for CLI // @@ -446,5 +446,21 @@ public class SingleValueObject { public string Value { get; set; } } + + [Test] + public void TestIssue252() + { + var context = new SerializationContext(); + var serializer = context.GetSerializer(); + var bytes = serializer.PackSingleObject( new Issue252Class() ); + Assert.That( bytes.Length, Is.EqualTo( MessagePackSerializer.BufferSize + 1 ), Binary.ToHexString( bytes ) ); + } + + public class Issue252Class + { + // BufferSize - sizeof( Int32 property ) - sizeof( byte array type header with 1bit length ) - sizeof( array header ); + public byte[] ByteArray = new byte[ MessagePackSerializer.BufferSize - sizeof( int ) - sizeof( byte ) - 1 - 1 ]; + public int Int32 = Int32.MaxValue; + } } }