iOSSwiftStabilityFlick

Five Bugs Caught by an 8-Way Adversarial Review Before Flick 2.0.1

An 8-subagent adversarial code review right before Flick 2.0.1 (commit 00a4aa1) uncovered a silent byte-cap bypass on iOS 17–26, a zoom-resize focus snap, orphaned undo batches, an integer-overflow crash in tampered ledgers, and solid-color images flagged as blurry.

Right before shipping Flick 2.0.1, I ran an 8-way parallel adversarial review across the codebase (commit 00a4aa1). Everything looked fine in normal device testing, yet reading specifically for broken invariants, OS version gaps, hostile inputs, and torn state caught five real bugs.

1. The 100 MB Free Byte Cap Never Bound on iOS 17–26

PhotoAsset.swift computed resource sizes like this:

if #available(iOS 27, macOS 27, *) {
    knownSizes = resources.compactMap { $0.dataSize.map(Int64.init) }
}

PHAssetResource.dataSize was introduced in iOS 27, and the new SDK removed fileSize from the public Swift interface. With no else branch, knownSizes evaluated to [] on iOS 17 through iOS 26, making every asset’s quotaBytes evaluate to 0. The 100 MB free-tier byte limit never triggered.

Adding the fallback branch—reading ($0.value(forKey: "fileSize") as? NSNumber)?.int64Value via KVC, the exact key releases through v1.8 used—restored real byte counting on iOS 17–26, while flagging unreadable resources as estimated instead of silently charging 0.

2. Resizing a Zoomed Photo Snapped Focus Back to Top-Left

After a window resize or orientation change, ZoomImageScrollView.swift calls restoreFocus(_:) to keep the inspected region centered.

It was clamping contentOffset against the unzoomed contentSize, even though contentOffset lives in the rendered (zoomed) coordinate space—just as centerContent() computes centering insets from zoomed bounds. On resize while zoomed in, the tighter unzoomed clamp collapsed the offset to the top-left corner.

Replacing the hand-rolled math with renderedSize = imageView.convert(imageView.bounds, to: self).size passed into ZoomViewportTransform.contentOffset(focus:imageSize:viewport:) kept geometry in one tested helper.

3. Restoring From the Pending Bin Left Phantom Progress—and Orphaned Undo Batches

CleanupSession.swift and PendingBinScreen.swift had three state-drift bugs:

  • Phantom +1 in progress: Removing a photo from the Pending Bin (session.restore(_:)) updated pending and reviewedIDs, but never called ProgressTracker.undoLastDecision(recordedAt:). The “Reviewed” counter kept a phantom +1. restore(_:) now returns the original decision’s recordedAt date so callers roll back ProgressTracker in lockstep.
  • Orphaned batch undo entries: Staging a duplicate group appended transient .delete records to history. Dismissing the undo banner (dismissBatchFeedback()) or staging another batch left those .delete entries behind—leaving nextUndoAssetID pointing at an already-staged photo and disabling single-step undo on the swipe deck. Both paths now purge transient batch history via removeBatchHistory(_:).
  • Torn backup wipeout: reviewedBackup.v1 used synthesized Codable. If the app was killed mid-write after ids flushed but before removedIDs and isComplete were written, the next launch threw keyNotFound and threw away the user’s entire review history. Switching to a hand-written init(from:) with decodeIfPresent recovers ids cleanly.

4. A Corrupted Allowance Ledger Could Trap on Launch

FreeCleanupUsage is persisted in the Keychain, UserDefaults, and iCloud KVS—all user-writable on a jailbroken device.

If a corrupted or tampered payload stored Int.max or Int64.max, usedPhotos + batchPhotos in FreeCleanupPolicy.allowsDelete and grow-only merges would trap under Swift’s checked + operator and crash the app.

Two changes closed it:

  1. FreeCleanupUsage.init clamps persisted values to maxCount = 10_000_000 and maxBytes = 1 << 40 (1 TiB), far above any legitimate free-tier ledger.
  2. FreeCleanupPolicy.allowsDelete now compares against remaining headroom in subtraction form: batchPhotos <= freePhotoLimit - min(usedPhotos, freePhotoLimit).

While reviewing FreeCleanupCloudKV, we also noticed writes overwrote baseline directly instead of max-merging with the existing KVS record, letting a device with a stale v1 total downgrade the shared baseline.

5. Solid-Color Frames Scored 0.0 and Got Pre-Selected as Blurry

BlurScorer.swift divides Laplacian variance by grayscale variance. A textureless frame—an accidental lens-cap shot, a blank whiteboard—has lapVar near 0.

Returning 0.0 caused the classifier to treat it as “definitively blurry” and pre-select it for deletion. Blur is undefined without texture, so guard lapVar > 1e-9 else { return nil } now reports it as unknown instead. At the same time, lowering the imgVar floor from 1.0 to 1e-6 fixed false blur flags on sharp low-contrast images like blue skies and white walls, whose normalized UInt8 variance was below 1.0.

Two Bonus Hardening Fixes

The same pass moved AnalysisCoordinator’s consecutiveLoadFailures = 0 reset inside runTask (ending a MainActor race with a previously cancelled task), and added a Swift source scan to scripts/check-privacy.py so calling systemUptime, attributesOfFileSystem, or activeInputModes without declaring them in PrivacyInfo.xcprivacy fails CI instead of App Store Connect upload.

Happy-path tests rarely catch an Int.max plist or a missing #available else branch—explicitly looking for ways to break your own invariants does.

✦

Comments

  • Loading…

Comments are reviewed before publishing; email is visible only to me.