Skip to content

Commit eb2df02

Browse files
authored
Adds band-aid for possible arithmetic overflow (#127)
1 parent ab94255 commit eb2df02

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

AudioStreaming/Streaming/AudioPlayer/Processors/AudioPlayerRenderProcessor.swift

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ final class AudioPlayerRenderProcessor: NSObject {
6565
let frameSizeInBytes = bufferContext.sizeInBytes
6666
let used = bufferContext.frameUsedCount
6767
let start = bufferContext.frameStartIndex
68+
#if DEBUG
69+
assert(start < bufferContext.totalFrameCount,
70+
"frameStartIndex (\(start)) >= totalFrameCount (\(bufferContext.totalFrameCount)) - logic error!")
71+
assert(used <= bufferContext.totalFrameCount,
72+
"frameUsedCount (\(used)) > totalFrameCount (\(bufferContext.totalFrameCount)) - logic error!")
73+
#endif
6874
let end = (bufferContext.frameStartIndex + bufferContext.frameUsedCount) % bufferContext.totalFrameCount
6975
let signal = rendererContext.waiting.value && used < bufferContext.totalFrameCount / 2
7076

@@ -122,12 +128,33 @@ final class AudioPlayerRenderProcessor: NSObject {
122128
totalFramesCopied = framesToCopy
123129

124130
rendererContext.lock.lock()
131+
#if DEBUG
132+
if totalFramesCopied > bufferContext.frameUsedCount {
133+
Logger.debug("Buffer race: tried to consume %d frames but only %d available (reset likely occurred)",
134+
category: .audioRendering,
135+
args: totalFramesCopied, bufferContext.frameUsedCount)
136+
}
137+
#endif
125138
bufferContext.frameStartIndex = (bufferContext.frameStartIndex + totalFramesCopied) % bufferContext.totalFrameCount
126-
bufferContext.frameUsedCount -= totalFramesCopied
139+
if totalFramesCopied <= bufferContext.frameUsedCount {
140+
bufferContext.frameUsedCount -= totalFramesCopied
141+
} else {
142+
bufferContext.frameUsedCount = 0
143+
}
127144
rendererContext.lock.unlock()
128145

129146
} else {
130-
let frameToCopy = min(inNumberFrames, bufferContext.totalFrameCount - start)
147+
let frameToCopy: UInt32
148+
if start < bufferContext.totalFrameCount {
149+
frameToCopy = min(inNumberFrames, bufferContext.totalFrameCount - start)
150+
} else {
151+
#if DEBUG
152+
Logger.debug("Buffer race: start index %d >= totalFrameCount %d (reset likely occurred)",
153+
category: .audioRendering,
154+
args: start, bufferContext.totalFrameCount)
155+
#endif
156+
frameToCopy = 0
157+
}
131158
bufferList.mBuffers.mNumberChannels = 2
132159
bufferList.mBuffers.mDataByteSize = frameSizeInBytes * frameToCopy
133160

@@ -160,8 +187,19 @@ final class AudioPlayerRenderProcessor: NSObject {
160187
totalFramesCopied = frameToCopy + moreFramesToCopy
161188

162189
rendererContext.lock.lock()
190+
#if DEBUG
191+
if totalFramesCopied > bufferContext.frameUsedCount {
192+
Logger.debug("Buffer race: tried to consume %d frames but only %d available (reset likely occurred)",
193+
category: .audioRendering,
194+
args: totalFramesCopied, bufferContext.frameUsedCount)
195+
}
196+
#endif
163197
bufferContext.frameStartIndex = (bufferContext.frameStartIndex + totalFramesCopied) % bufferContext.totalFrameCount
164-
bufferContext.frameUsedCount -= totalFramesCopied
198+
if totalFramesCopied <= bufferContext.frameUsedCount {
199+
bufferContext.frameUsedCount -= totalFramesCopied
200+
} else {
201+
bufferContext.frameUsedCount = 0
202+
}
165203
rendererContext.lock.unlock()
166204
}
167205

AudioStreaming/Streaming/Helpers/BufferContext.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ final class BufferContext {
1313
var frameUsedCount: UInt32 = 0
1414

1515
var end: UInt32 {
16-
(frameStartIndex + frameUsedCount) % totalFrameCount
16+
#if DEBUG
17+
assert(frameStartIndex < totalFrameCount,
18+
"frameStartIndex (\(frameStartIndex)) exceeds totalFrameCount (\(totalFrameCount)) - logic error!")
19+
assert(frameUsedCount <= totalFrameCount,
20+
"frameUsedCount (\(frameUsedCount)) exceeds totalFrameCount (\(totalFrameCount)) - logic error!")
21+
#endif
22+
return (frameStartIndex + frameUsedCount) % totalFrameCount
1723
}
1824

1925
init(sizeInBytes: UInt32, totalFrameCount: UInt32) {

0 commit comments

Comments
 (0)