Fix crash in filesFromStatus: when piece bitfield is empty#2
Open
mrowlinson wants to merge 1 commit into
Open
Conversation
When a torrent has just been added and the piece check has not yet populated stat.pieces, the bitfield's backing storage can be empty. Calling stat.pieces.get_bit(index) in that state dereferences null internal storage, crashing with EXC_BAD_ACCESS at a small address (0x4 in the trace below). Bounds-check each piece index against stat.pieces.size() and fall back to @no when the bitfield does not yet cover the index. Once the piece check completes the snapshot will be refreshed with the real bits. Reproduces in 2.0.11 by adding any new torrent: TorrentHandle.prepareToAdd triggers updateSnapshot synchronously, which calls filesFromStatus on a status snapshot whose pieces bitfield has not been initialised yet.
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.
Summary
filesFromStatus:callsstat.pieces.get_bit(index)for every piece of every file. When a torrent has just been added and the piece check has not yet populatedstat.pieces, the bitfield's backing storage is empty andget_bitdereferences null internal storage, crashing withEXC_BAD_ACCESS / KERN_INVALID_ADDRESS at 0x4.This patch bounds-checks each piece index against
stat.pieces.size()and falls back to@NOfor indices the bitfield does not yet cover. Once the piece check populates the bitfield the snapshot is refreshed with the real bits.Reproduction
LibTorrent-Swift 2.0.11 (commit
70ca1a7) inside a host iTorrent build, on iOS 26.4.2, iPhone 17 Pro Max:TorrentHandle.prepareToAddrunsupdateSnapshotsynchronously, which callsfilesFromStatus:on a status snapshot whosepiecesbitfield is still empty → crash.Top of the faulting stack from the
.ipsreport:Test plan
xcodebuild -workspace ... -scheme iTorrent -destination 'generic/platform=iOS' build)