Possible buffer overrun in synth sample playback

,

Platform: Mac Simulator

I've noticed periodic crashes when playing sample based synths, and it seems to occur when the play time is longer than the (rate adjusted) sample itself. Sometimes nothing unusual happens, sometimes it ends up playing loud, glitchy garbage after reaching the end of the sample, and sometimes it crashes.

Here's a simple example that triggers the bad behavior fairly consistently

local snd = playdate.sound
-- 5 second sample at C4
local sample = snd.sample.new("strings.wav")

function stringsynth()
  local s = snd.synth.new(sample)
  s:setVolume(0.5)
  s:setAttack(0.0)
  s:setDecay(0)
  s:setSustain(1)
  s:setRelease(0)
  return s
end

local synth = stringsynth()
synth:playNote("C4", 15)

function playdate.update()
end

And here's an example of the crashed thread:

Process:               Playdate Simulator [78347]
Path:                  /Users/USER/*/Playdate Simulator.app/Contents/MacOS/Playdate Simulator
Identifier:            date.play.simulator
Version:               1.12.2 (139389)
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           Playdate Simulator [78347]
User ID:               501

Date/Time:             2022-08-06 12:44:31.167 -0700
OS Version:            macOS 11.6.8 (20G730)
Report Version:        12
Anonymous UUID:        A71DD14E-7A35-311E-933E-D13BF557273C

Sleep/Wake UUID:       62D90C6B-4934-4C53-BFAA-CDBDF2C39E50

Time Awake Since Boot: 380000 seconds
Time Since Wake:       10000 seconds

System Integrity Protection: enabled

Crashed Thread:        16  com.apple.audio.IOThread.client

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x00007fbd40fb2000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [78347]
...

Thread 16 Crashed:: com.apple.audio.IOThread.client
0   date.play.simulator           	0x000000010156cac0 fillSampleBuffer + 1388
1   date.play.simulator           	0x00000001015685d8 renderSample + 173
2   date.play.simulator           	0x0000000101567953 PDSynth_render + 1527
3   date.play.simulator           	0x000000010156b3a2 SoundLib_render + 633
4   date.play.simulator           	0x00000001015a9006 -[PCGameView renderAudio:::] + 341
5   date.play.simulator           	0x00000001015a9a56 __24-[PCGameView startAudio]_block_invoke_2 + 39
6   com.apple.audio.AVFAudio      	0x00007fff6e0d5369 __51+[AVAudioSourceNode pullInputBlockFromRenderBlock:]_block_invoke + 34
7   com.apple.audio.AVFAudio      	0x00007fff6e0cc387 invocation function for block in AUGraphSourceNodeV3::AllocateInputBlock() + 20
2 Likes