From e56fe75d88f48f31a5e1d48f6b863ebf70c8b817 Mon Sep 17 00:00:00 2001 From: Jay Jones Date: Wed, 22 Jul 2026 15:45:12 -0400 Subject: [PATCH] fix: prevent integer overflow in AudioSourceFactory frame count calculation Fixes a crash when processing audio files where (audioFile.length - audioFile.framePosition) exceeds AVAudioFrameCount.max (UInt32.max = ~4.29 billion frames). The bug caused a Swift runtime failure: "Not enough bits to represent the passed value" when casting Int64 to UInt32 without bounds checking. This occurred during initialization in apps using FluidAudio for diarization, particularly when Speech API capability checks triggered audio processing. Solution: Clamp the remaining frames to AVAudioFrameCount.max before casting, preventing overflow while maintaining correct behavior for normal-sized audio files. --- Sources/FluidAudio/Shared/AudioSourceFactory.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/FluidAudio/Shared/AudioSourceFactory.swift b/Sources/FluidAudio/Shared/AudioSourceFactory.swift index e7f4090c0..2cbf12c8f 100644 --- a/Sources/FluidAudio/Shared/AudioSourceFactory.swift +++ b/Sources/FluidAudio/Shared/AudioSourceFactory.swift @@ -135,7 +135,9 @@ public struct AudioSourceFactory { } do { - let remainingFrames = AVAudioFrameCount(audioFile.length - audioFile.framePosition) + // Clamp to AVAudioFrameCount.max to prevent overflow when casting from Int64 + let remainingFramesInt64 = audioFile.length - audioFile.framePosition + let remainingFrames = AVAudioFrameCount(min(remainingFramesInt64, Int64(AVAudioFrameCount.max))) let framesToRead = min(inputCapacity, remainingFrames) if framesToRead > 0 { try audioFile.read(into: capturedInputBuffer, frameCount: framesToRead)