Fix panics when extracting or reading an index of a zero-length file#362
Merged
Conversation
An index created from an empty file contains no chunks. AssembleFile unconditionally built a plan segment from Chunks[0] and panicked with a slice bounds error in the worker, and NewIndexReadSeeker dereferenced Chunks[0] directly. This crashed "desync extract", "desync cat" and "desync mount-index" on such indexes even though "desync make" creates them without complaint. AssembleFile now returns right after creating and truncating the output file when the index has no chunks. The index read-seeker tolerates an empty chunk list in its constructor, seek path and read path.
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.
An index created from an empty file contains no chunks (
desync makeproduces one without complaint), but reading such an index back panicked:desync extractcrashed withslice bounds out of range [:1] with capacity 0—SeedSequencer.Next()always emits a segment{first: 0, last: 0}even for an empty chunk list, and the assemble worker then slicesChunks[0:1].desync catanddesync mount-indexcrashed withindex out of range [0]inNewIndexReadSeeker, which readsChunks[0].IDunconditionally.Reproducer:
Fixes:
AssembleFile()returns right after creating and truncating the output file when the index has no chunks.NewIndexReadSeeker()tolerates an empty chunk list;findOffset()just tracks the position for an empty index;Read()returns EOF at or past the end.Both paths verified with the reproducer above and covered by new tests.