Fix Int overflow in maxTotalSubFrames on 32-bit platforms (wasm32, watchOS) - #88
Open
mansbernhardt wants to merge 1 commit into
Open
Fix Int overflow in maxTotalSubFrames on 32-bit platforms (wasm32, watchOS)#88mansbernhardt wants to merge 1 commit into
mansbernhardt wants to merge 1 commit into
Conversation
`maxTotalSubFrames(in:base:)` computed `maxTotalFrames(in: extent) * base.rawValue` directly in `Int`. With `extent == .max100Days` that product exceeds `Int32.max` for every frame rate — the smallest case, 23.976 fps at 80 subframes, is already 2_073_600 * 100 * 80 = 16_588_800_000 against an `Int.max` of 2_147_483_647 — so the multiplication traps on any 32-bit platform (wasm32, watchOS armv7k/arm64_32). Because the bound is recomputed inside every wrapping add (`sfcNew.clamped(to: 0 ... maxSubFrameCountExpressible)`), all arithmetic on a `.max100Days` timecode trapped on those platforms however small the operands. Compute the product in `Int64` and saturate on return. No behaviour change on 64-bit: the product peaks at ~82.9e9, far below `Int64.max`, so the clamp never engages. Correct on 32-bit: the value is only ever used as an upper bound, and a `subFrameCount` that large is itself unrepresentable there, so `Int.max` still bounds the whole representable domain. Signature unchanged. Adds two regression tests covering every frame rate and subframe base, and a `.max100Days` wrapping add.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
TimecodeFrameRate.maxTotalSubFrames(in:base:)computes its product directly inInt:With
extent == .max100Daysthat product exceedsInt32.maxfor every frame rate. The smallest case, 23.976 fps at 80 subframes, is alreadyso the multiplication traps on overflow on any 32-bit platform — wasm32, and watchOS
armv7k/arm64_32.Because the bound is recomputed inside every wrapping add (
sfcNew.clamped(to: 0 ... maxSubFrameCountExpressible)), this makes all arithmetic on a.max100Daystimecode trap on those platforms, no matter how small the operands are.Repro (wasm32)
Observed in a browser, wasm32 debug build:
Construction, comparison,
max(by:)and.realTimeValueall work; only arithmetic under.max100Daystraps.Worth noting this is easy to hit without ever choosing
.max100Daysdeliberately: our wrapper type sets it on everyTimecodeit constructs, so every timecode operation trapped once we started building for wasm32.The fix
Compute in
Int64, saturate on return:Int64.max, so the clamp never engages. The existing exact-value assertions inTimecodeFrameRate_Properties_Tests.properties()still hold.clamped(to:)range, or a>comparison against asubFrameCount. AsubFrameCountthat large is itself unrepresentable in a 32-bitInt, so saturating atInt.maxstill bounds the entire representable domain.Tests
Two regression tests in
TimecodeFrameRate Properties Tests.swift:maxTotalSubFramesDoesNotOverflowOn32Bit()— every frame rate × every subframe base at.max100Days; asserts the exact product on 64-bit andInt.maxon 32-bit, and thatmaxSubFrameCountExpressiblestays consistent.max100DaysArithmeticDoesNotTrap()— the wrapping add above.Full suite green locally: 506 tests in 55 suites passed.
One suggestion, happy to do it separately
The wasm CI jobs added in #87 (mine) run
swift buildonly. This defect compiles perfectly and traps at runtime, so a build-only job structurally cannot catch it — and the tests above would have, had the suite run under wasm32. If you'd like, I can follow up with a PR that runsswift teston the wasm jobs via wasmtime or Node.