VoiceInk icon indicating copy to clipboard operation
VoiceInk copied to clipboard

Crash - Force unwrap of audio buffer pointers without validation

Open ebrindley opened this issue 1 month ago • 0 comments

Crash - Force unwrap of audio buffer pointers without validation

Labels: bug, crash, audio-pipeline, medium-priority

Description

AudioFileProcessor force-unwraps buffer pointers without validation, which crashes if buffer allocation fails or audio format is unexpected.

User Impact

  • Crashes when saving audio files under memory pressure

  • Data loss if crash occurs during recording save operation

  • No graceful degradation or error recovery

Technical Details

File: VoiceInk/Services/AudioFileProcessor.swift

Lines: 170-171


let int16Pointer = int16Buffer.baseAddress!

buffer.int16ChannelData![0].update(from: int16Pointer, count: int16Samples.count)

Two force unwraps without validation:

  1. int16Buffer.baseAddress! - crashes if allocation failed

  2. buffer.int16ChannelData![0] - crashes if no channel data

Recommended Fix

Replace lines 169-172 in VoiceInk/Services/AudioFileProcessor.swift:


// Convert float samples to int16

let int16Samples = samples.map { max(-1.0, min(1.0, $0)) * Float(Int16.max) }.map { Int16($0) }

 

// Copy samples to buffer with proper validation

int16Samples.withUnsafeBufferPointer { int16Buffer in

    guard let int16Pointer = int16Buffer.baseAddress else {

        throw AudioProcessingError.conversionFailed

    }

 

    guard let channelData = buffer.int16ChannelData,

          buffer.format.channelCount > 0 else {

        throw AudioProcessingError.conversionFailed

    }

 

    channelData[0].update(from: int16Pointer, count: int16Samples.count)

}

buffer.frameLength = AVAudioFrameCount(samples.count)

The AudioProcessingError enum already exists in the file with a conversionFailed case, so no additional error types need to be defined.

Testing

  1. Normal audio saving should work identically

  2. Simulate memory pressure and verify graceful error handling

  3. Test with various audio formats and buffer sizes

  4. Verify appropriate error messages when failures occur

ebrindley avatar Nov 12 '25 18:11 ebrindley