Crash - Force unwrap of audio buffer pointers without validation
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:
-
int16Buffer.baseAddress!- crashes if allocation failed -
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
-
Normal audio saving should work identically
-
Simulate memory pressure and verify graceful error handling
-
Test with various audio formats and buffer sizes
-
Verify appropriate error messages when failures occur