Skip to content

Fix panics when extracting or reading an index of a zero-length file#362

Merged
folbricht merged 1 commit into
masterfrom
fix-empty-index-panic
Jul 5, 2026
Merged

Fix panics when extracting or reading an index of a zero-length file#362
folbricht merged 1 commit into
masterfrom
fix-empty-index-panic

Conversation

@folbricht

Copy link
Copy Markdown
Owner

An index created from an empty file contains no chunks (desync make produces one without complaint), but reading such an index back panicked:

  • desync extract crashed with slice bounds out of range [:1] with capacity 0SeedSequencer.Next() always emits a segment {first: 0, last: 0} even for an empty chunk list, and the assemble worker then slices Chunks[0:1].
  • desync cat and desync mount-index crashed with index out of range [0] in NewIndexReadSeeker, which reads Chunks[0].ID unconditionally.

Reproducer:

$ : > empty.bin
$ desync make -s ./store empty.caibx empty.bin
$ desync extract -s ./store empty.caibx out.bin
panic: runtime error: slice bounds out of range [:1] with capacity 0
$ desync cat -s ./store empty.caibx
panic: runtime error: index out of range [0] with length 0

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.

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.
@folbricht folbricht merged commit a745957 into master Jul 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant