Skip to content

Commit

Permalink
MIDIPacket Pre-allocate Bytes (#2891)
Browse files Browse the repository at this point in the history
* Add test for #2890

* Fix heap overflow #2890

Pre-allocate needed bytes so heap doesn't overflow

* Hound
  • Loading branch information
emurray2 committed Feb 23, 2024
1 parent c5db1e4 commit 5d7c325
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/AudioKit/MIDI/MIDI+Sending.swift
Expand Up @@ -356,7 +356,8 @@ extension MIDI {
time: MIDITimeStamp = mach_absolute_time(),
endpointsUIDs: [MIDIUniqueID]? = nil,
virtualOutputPorts: [MIDIPortRef]? = nil) {
let packetListPointer: UnsafeMutablePointer<MIDIPacketList> = UnsafeMutablePointer.allocate(capacity: 1)
let packetListPointer: UnsafeMutablePointer<MIDIPacketList> = UnsafeMutablePointer.allocate(
capacity: data.count)

var packet: UnsafeMutablePointer<MIDIPacket> = MIDIPacketListInit(packetListPointer)
packet = MIDIPacketListAdd(packetListPointer, 1_024, packet, time, data.count, data)
Expand Down
7 changes: 7 additions & 0 deletions Tests/AudioKitTests/MIDI Tests/UMPParsingTests.swift
Expand Up @@ -121,6 +121,13 @@ class UMPParsingTests: XCTestCase {
portID: sender.uniqueID)])
}

// Test for heap overflow encountered in https://github.com/AudioKit/AudioKit/issues/2890
// will timeout if heap overflow is encountered
func testHeapAllocation() {
let overflowingMessage: [UInt8] = Array(repeating: 0x01, count: 260)
midi.sendMessage(overflowingMessage)
}

func testSimultaneousStreams() throws {
throw XCTSkip("skip test for now: sysex joining is not thread safe")

Expand Down

0 comments on commit 5d7c325

Please sign in to comment.