Skip to content

Commit 512aca0

Browse files
authored
Merge pull request #256 from msgpack/fix/#252
I confirmed branch CI log shows "build succeeded" but never ends...
2 parents 8e14d41 + b7467e9 commit 512aca0

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

CHANGES.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,9 @@ Release 0.9.0 2017-8-26
701701
* Fix enum serialization throws NullReferenceException in Unity. Issue #215.
702702
* Fix MessagePackSerializer.Capability does not work correctly in Unity.
703703
* Fix polymorphic serializer error in Unity.
704+
705+
Release 0.9.1 2017-8-30
706+
707+
BUG FIXES
708+
* Fix ByteArrayPacker throws IndexOutOfBoundException when the buffer remaining bytes is equal to packed scalar size. #252
709+

build/Version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.0
1+
0.9.1

src/MsgPack/MessagePackByteArrayPacker.Pack.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
22
#region -- License Terms --
33
//
44
// MessagePack for CLI
@@ -981,9 +981,10 @@ private void WriteBytes( byte header, byte value )
981981
var buffer = this._buffer;
982982
var offset = this._offset;
983983
var remains = buffer.Length - offset;
984-
if ( remains < sizeof( byte ) && !this._allocator.TryAllocate( buffer, sizeof( byte ), out buffer ) )
984+
const int requiredSize = sizeof( byte ) + 1;
985+
if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) )
985986
{
986-
this.ThrowEofException( sizeof( byte ) );
987+
this.ThrowEofException( requiredSize );
987988
}
988989

989990
buffer[ offset ] = header;
@@ -1000,9 +1001,10 @@ private void WriteBytes( byte header, ushort value )
10001001
var buffer = this._buffer;
10011002
var offset = this._offset;
10021003
var remains = buffer.Length - offset;
1003-
if ( remains < sizeof( ushort ) && !this._allocator.TryAllocate( buffer, sizeof( ushort ), out buffer ) )
1004+
const int requiredSize = sizeof( ushort ) + 1;
1005+
if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) )
10041006
{
1005-
this.ThrowEofException( sizeof( ushort ) );
1007+
this.ThrowEofException( requiredSize );
10061008
}
10071009

10081010
buffer[ offset ] = header;
@@ -1020,9 +1022,10 @@ private void WriteBytes( byte header, uint value )
10201022
var buffer = this._buffer;
10211023
var offset = this._offset;
10221024
var remains = buffer.Length - offset;
1023-
if ( remains < sizeof( uint ) && !this._allocator.TryAllocate( buffer, sizeof( uint ), out buffer ) )
1025+
const int requiredSize = sizeof( uint ) + 1;
1026+
if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) )
10241027
{
1025-
this.ThrowEofException( sizeof( uint ) );
1028+
this.ThrowEofException( requiredSize );
10261029
}
10271030

10281031
buffer[ offset ] = header;
@@ -1042,9 +1045,10 @@ private void WriteBytes( byte header, ulong value )
10421045
var buffer = this._buffer;
10431046
var offset = this._offset;
10441047
var remains = buffer.Length - offset;
1045-
if ( remains < sizeof( ulong ) && !this._allocator.TryAllocate( buffer, sizeof( ulong ), out buffer ) )
1048+
const int requiredSize = sizeof( ulong ) + 1;
1049+
if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) )
10461050
{
1047-
this.ThrowEofException( sizeof( ulong ) );
1051+
this.ThrowEofException( requiredSize );
10481052
}
10491053

10501054
buffer[ offset ] = header;
@@ -1069,9 +1073,10 @@ private void WriteBytes( byte header, float value )
10691073
var buffer = this._buffer;
10701074
var offset = this._offset;
10711075
var remains = buffer.Length - offset;
1072-
if ( remains < sizeof( float ) && !this._allocator.TryAllocate( buffer, sizeof( float ), out buffer ) )
1076+
const int requiredSize = sizeof( float ) + 1;
1077+
if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) )
10731078
{
1074-
this.ThrowEofException( sizeof( float ) );
1079+
this.ThrowEofException( requiredSize );
10751080
}
10761081

10771082
buffer[ offset ] = header;
@@ -1092,9 +1097,10 @@ private void WriteBytes( byte header, double value )
10921097
var buffer = this._buffer;
10931098
var offset = this._offset;
10941099
var remains = buffer.Length - offset;
1095-
if ( remains < sizeof( double ) && !this._allocator.TryAllocate( buffer, sizeof( double ), out buffer ) )
1100+
const int requiredSize = sizeof( double ) + 1;
1101+
if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) )
10961102
{
1097-
this.ThrowEofException( sizeof( double ) );
1103+
this.ThrowEofException( requiredSize );
10981104
}
10991105

11001106
buffer[ offset ] = header;

src/MsgPack/MessagePackByteArrayPacker.Pack.tt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<#@ template debug="true" hostSpecific="true" language="C#" #>
1+
<#@ template debug="true" hostSpecific="true" language="C#" #>
22
<#@ output extension=".cs" #>
33
<#@ Assembly Name="System.Core.dll" #>
44
<#@ import namespace="System" #>
@@ -77,9 +77,10 @@ namespace MsgPack
7777
var buffer = this._buffer;
7878
var offset = this._offset;
7979
var remains = buffer.Length - offset;
80-
if ( remains < sizeof( <#= type #> ) && !this._allocator.TryAllocate( buffer, sizeof( <#= type #> ), out buffer ) )
80+
const int requiredSize = sizeof( <#= type #> ) + 1;
81+
if ( remains < requiredSize && !this._allocator.TryAllocate( buffer, requiredSize, out buffer ) )
8182
{
82-
this.ThrowEofException( sizeof( <#= type #> ) );
83+
this.ThrowEofException( requiredSize );
8384
}
8485

8586
buffer[ offset ] = header;

test/MsgPack.UnitTest/Serialization/RegressionTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region -- License Terms --
1+
#region -- License Terms --
22
//
33
// MessagePack for CLI
44
//
@@ -446,5 +446,21 @@ public class SingleValueObject
446446
{
447447
public string Value { get; set; }
448448
}
449+
450+
[Test]
451+
public void TestIssue252()
452+
{
453+
var context = new SerializationContext();
454+
var serializer = context.GetSerializer<Issue252Class>();
455+
var bytes = serializer.PackSingleObject( new Issue252Class() );
456+
Assert.That( bytes.Length, Is.EqualTo( MessagePackSerializer.BufferSize + 1 ), Binary.ToHexString( bytes ) );
457+
}
458+
459+
public class Issue252Class
460+
{
461+
// BufferSize - sizeof( Int32 property ) - sizeof( byte array type header with 1bit length ) - sizeof( array header );
462+
public byte[] ByteArray = new byte[ MessagePackSerializer.BufferSize - sizeof( int ) - sizeof( byte ) - 1 - 1 ];
463+
public int Int32 = Int32.MaxValue;
464+
}
449465
}
450466
}

0 commit comments

Comments
 (0)