Skip to content

Reject truncated BGZF blocks instead of treating them as EOF - #717

Open
thierrygosselin wants to merge 2 commits into
OpenGene:masterfrom
thierrygosselin:fix-truncated-bgzf-input
Open

thierrygosselin wants to merge 2 commits into
OpenGene:masterfrom
thierrygosselin:fix-truncated-bgzf-input

Conversation

@thierrygosselin

Copy link
Copy Markdown

Summary

The BGZF reader currently treats incomplete block headers and bodies as normal end-of-file. Consequently, truncated input can produce incomplete output while fastp exits successfully.

This PR distinguishes these failures from clean EOF.

Changes

  • Reject partial BGZF headers and bodies with explicit errors.
  • Check input-stream read errors.
  • Reject undersized blocks before subtracting the header length, preventing unsigned underflow.
  • Preserve existing handling of embedded EOF markers and complete streams without a terminal EOF marker.

This is separate from the concatenated-stream fix in #712 and does not depend on #716.

Tests

Added synthetic regression tests covering:

  • Valid BGZF input.
  • Complete input without a terminal EOF marker.
  • Concatenated BGZF streams.
  • Partial headers, payloads, and trailers.
  • Undersized blocks.

Each case runs with -w 1 and -w 4.

python3 scripts/test_bgzf_truncated_input.py ./fastp

On macOS ARM64, all 14 cases pass with the patch. The unchanged BGZF reader incorrectly exits successfully for all eight malformed-input cases.

Scope

This patch addresses block framing and read failures. It does not implement comprehensive gzip-header, CRC, or decompression-error validation. Linux execution and injected input-stream I/O errors have not been tested.

Distinguish normal end-of-file from incomplete BGZF headers and bodies.
Report input read errors explicitly instead of treating them as EOF.

Validate block sizes before subtracting the header length, preventing
unsigned underflow for undersized blocks.

Preserve support for concatenated BGZF streams and complete streams
without a terminal EOF marker.
Test incomplete headers, payloads, trailers, and undersized blocks using
synthetic BGZF fixtures with one and four processing threads.

Verify that valid streams, concatenated streams, and complete streams
without a terminal EOF marker preserve the input reads.

All 14 cases pass with the fix. The unchanged BGZF reader incorrectly
returns success for all eight malformed-input cases.
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