Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy packets to prevent AddressSanitizer: stack-use-after-return on address #2911

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions Sources/AudioKit/MIDI/Packets/MIDIPacketList+SequenceType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@ extension MIDIPacketList: Sequence {
/// Create the sequence
/// - Returns: Iterator of elements
public func makeIterator() -> AnyIterator<Element> {

withUnsafePointer(to: packet) { ptr in
// Copy packets to prevent AddressSanitizer: stack-use-after-return on address
let packets = withUnsafePointer(to: packet) { ptr in
var packets = [MIDIPacket]()
var p = ptr
var idx: UInt32 = 0

return AnyIterator {
guard idx < self.numPackets else {
return nil
for _ in 0..<numPackets {
if let packet = extractPacket(p) {
packets.append(packet)
} else {
// If a packet cannot be extracted, return already extracted and ignore all
// subsequent packets.
return packets
}

idx += 1
let packet = extractPacket(p)
p = UnsafePointer(MIDIPacketNext(p))
return packet
}

return packets
}

return AnyIterator(packets.makeIterator())
}
}

Expand Down