Fix sending MidiPacket when MidiPacket has been created using a pointer (#18981)

Fix sending MidiPacket when MidiPacket has been created using a pointer to a byte[] rather than a byte[] passed as a reference.

MidiPacket.bytes used to be initialized to null, but that changed in a null-refactoring. Unfortunately code that dependend on the initial value being null wasn't updated, so the code regressed. Here we revert the old behavior by making the MidiPacket.bytes field nullable

Also add a test to make sure we don't regress this code again.

---------

Co-authored-by: Alex Soto <alex@alexsoto.me>
Co-authored-by: Rolf Bjarne Kvinge <rolf@xamarin.com>
This commit is contained in:
jamesdlow 2024-01-03 07:40:32 +13:00 коммит произвёл GitHub
Родитель e4ec40ff34
Коммит d9e52067e2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 49 добавлений и 2 удалений

Просмотреть файл

@ -791,7 +791,7 @@ namespace CoreMidi {
#if !COREBUILD
public long TimeStamp;
IntPtr byteptr;
byte [] bytes = Array.Empty<byte> ();
byte []? bytes;
int start;
public ushort Length;
@ -847,7 +847,7 @@ namespace CoreMidi {
byteptr = IntPtr.Zero;
}
internal byte [] ByteArray {
internal byte []? ByteArray {
get { return bytes; }
}

Просмотреть файл

@ -0,0 +1,47 @@
//
// Unit tests for MidiClient
//
// Authors:
// Rolf Bjarne Kvinge <rolf@xamarin.com>
//
// Copyright 2023 Microsoft Corp. All rights reserved.
//
#if !__TVOS__ && !__WATCHOS__
using System;
using System.Diagnostics;
using CoreMidi;
using Foundation;
using NUnit.Framework;
namespace MonoTouchFixtures.CoreMidi {
[TestFixture]
[Preserve (AllMembers = true)]
public class MidiClientTest {
[Test]
public void SendTest ()
{
if (Midi.DestinationCount <= 0)
Assert.Inconclusive ("No Midi Destinations");
using var ep = MidiEndpoint.GetDestination (0);
Assert.NotNull (ep, "EndPoint");
var mevent = new byte [] { 0x90, 0x40, 0x70 };
using var client = new MidiClient ($"outputclient-{Process.GetCurrentProcess ().Id}");
using var port = client.CreateOutputPort ($"outputport-{Process.GetCurrentProcess ().Id}");
unsafe {
fixed (byte* meventPtr = mevent) {
using var packet1 = new MidiPacket (0, (ushort) mevent.Length, (IntPtr) meventPtr);
using var packet2 = new MidiPacket (0, mevent);
using var packet3 = new MidiPacket (0, mevent, 0, mevent.Length);
var packets = new MidiPacket [] { packet1, packet2, packet3 };
port.Send (ep, packets);
}
}
}
}
}
#endif