Skip to content

Commit

Permalink
Merge pull request #256 from msgpack/fix/#252
Browse files Browse the repository at this point in the history
I confirmed branch CI log shows "build succeeded" but never ends...
  • Loading branch information
yfakariya authored Aug 30, 2017
2 parents 8e14d41 + b7467e9 commit 512aca0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,9 @@ Release 0.9.0 2017-8-26
* Fix enum serialization throws NullReferenceException in Unity. Issue #215.
* 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

2 changes: 1 addition & 1 deletion build/Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.0
0.9.1
32 changes: 19 additions & 13 deletions src/MsgPack/MessagePackByteArrayPacker.Pack.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


#region -- License Terms --
//
// MessagePack for CLI
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/MsgPack/MessagePackByteArrayPacker.Pack.tt
Original file line number Diff line number Diff line change
@@ -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" #>
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 17 additions & 1 deletion test/MsgPack.UnitTest/Serialization/RegressionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region -- License Terms --
#region -- License Terms --
//
// MessagePack for CLI
//
Expand Down Expand Up @@ -446,5 +446,21 @@ public class SingleValueObject
{
public string Value { get; set; }
}

[Test]
public void TestIssue252()
{
var context = new SerializationContext();
var serializer = context.GetSerializer<Issue252Class>();
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;
}
}
}

0 comments on commit 512aca0

Please sign in to comment.