Skip to content

Commit

Permalink
[foundation] Fix empty NSData.ToArray() crash (#4103)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunigaku authored and dalexsoto committed May 18, 2018
1 parent 4e00419 commit 07356b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Foundation/NSData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ internal NSData (IntPtr handle, bool owns)
public byte[] ToArray ()
{
var res = new byte [Length];
Marshal.Copy (Bytes, res, 0, res.Length);
if (Length > 0)
Marshal.Copy (Bytes, res, 0, res.Length);
return res;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/monotouch-test/Foundation/NSDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public void ToArray ()
}
}

[Test]
public void ToEmptyArray ()
{
using (var data = NSData.FromArray (new byte[0])) {
var arr = data.ToArray ();
Assert.AreEqual (0, arr.Length, "Length");
}
}

[Test]
public void BytesLength ()
{
Expand Down

0 comments on commit 07356b8

Please sign in to comment.