Skip to content

Instantly share code, notes, and snippets.

@armadsen
Created February 8, 2018 21:41
Show Gist options
  • Save armadsen/424a5427f77cb25c5abca6c7ce21564b to your computer and use it in GitHub Desktop.
Save armadsen/424a5427f77cb25c5abca6c7ce21564b to your computer and use it in GitHub Desktop.
Demonstrates progblem in https://openradar.appspot.com/37363662
/* Compile this with:
clang -std=c99 -framework CoreMIDI -fed MIDIPacketNextTest.m
Run:
./a.out
*/
#import <CoreMIDI/CoreMIDI.h>
int main(int argc, char *argv[]) {
MIDIPacket *packet1 = malloc(sizeof(MIDIPacket) + 5);
packet1->timeStamp = 0;
packet1->data[0] = 0x90; // Note on
packet1->data[1] = 0x3c;
packet1->data[2] = 0x40;
packet1->data[3] = 0xd0; // Channel Pressure
packet1->data[4] = 0x2a;
packet1->length = 5;
MIDIPacket *packet2 = malloc(sizeof(MIDIPacket) + 3);
packet2->timeStamp = 1;
packet2->data[0] = 0xb0; // Control Change
packet2->data[1] = 0x1b;
packet2->data[2] = 0x3f;
packet2->length = 3;
size_t listSize = sizeof(MIDIPacketList) + packet1->length + packet2->length;
MIDIPacketList *pktlist = malloc(listSize);
MIDIPacket *curPacket = MIDIPacketListInit(pktlist);
curPacket = MIDIPacketListAdd(pktlist, listSize, curPacket, packet1->timeStamp, packet1->length, packet1->data);
curPacket = MIDIPacketListAdd(pktlist, listSize, curPacket, packet2->timeStamp, packet2->length, packet2->data);
// Get the first packet
MIDIPacket *packet = (MIDIPacket *)pktlist->packet;
for (int i = 0; i < pktlist->numPackets; i++) {
// Ignore empty packets
if (packet->length == 0) { // UBSan flags this for bad pointer alignment on x86_64
printf("Packet was empty\n");
packet = MIDIPacketNext(packet);
continue;
}
packet = MIDIPacketNext(packet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment