Skip to content

Read wrapped FASTA and reject malformed input instead of looping forever - #105

Merged
jermp merged 7 commits into
jermp:masterfrom
ratschlab:upstream/malformed-input
Nov 28, 2026
Merged

jermp merged 7 commits into
jermp:masterfrom
ratschlab:upstream/malformed-input

Conversation

@adamant-pwn

Copy link
Copy Markdown
Contributor

The FASTA parser in encode_strings reads each record as one header line plus one sequence line and checks neither (only assert(n >= k), which is compiled out in Release builds). Wrapped (multi-line) FASTA is a common input, and with it the lines get paired out of step. Headers are encoded as sequence, and eventually a line shorter than k is taken as a sequence. Then n - k + 1 underflows, the per-sequence loop in compute_minimizer_tuples runs ~2^64 times, and each full buffer is flushed to a new sshash.tmp.run_*.minimizers.N.bin file. We hit this with a 100-transcript FASTA: about 1.4 TB of temp files in 20 minutes before the disk filled.

This PR:

  • reads wrapped FASTA: a record's sequence runs until the next > header or the end of the file, so its lines are joined. Single-line input is parsed as before. This applies to weighted input too, whose LN:i: length is then checked against the joined sequence;
  • makes encode_strings throw a std::runtime_error instead of looping or silently misreading when:
    • the first line is not a header starting with > (e.g. FASTQ);
    • a sequence is shorter than k;
    • the last sequence line has no terminating newline. Previously this record was silently dropped. Happy to accept it instead if you prefer;
    • a header is followed by another header or by the end of the file, i.e. a record has no sequence. The last one was also silently dropped;
    • a sequence contains a character outside the alphabet, e.g. N in DNA. Such characters were silently encoded as some other base or amino acid. The check is one table lookup per character while parsing.

The cf_seg format keeps its one-line-per-segment parsing. The --check reader in test/check_from_file.hpp reads FASTA records the same way, so it doesn't report false errors on wrapped input. Building ecoli1_k31_ust.fa.gz and with_weights/ecoli_sakai.ust.k31.fa.gz re-wrapped at 60 columns gives indexes byte-identical to the unwrapped builds, and --check passes on both.

test/test_malformed_input.cpp checks each rejection and that a wrapped record is read as one sequence. It lowers RLIMIT_FSIZE to 64 MiB, so a regression fails with SIGXFSZ instead of filling the CI runner's disk.

adamant-pwn and others added 3 commits October 28, 2026 10:27
The parser reads each record as one header line plus one sequence line
and did not check either. A sequence line shorter than k underflowed
its k-mer count (n - k + 1), so compute_minimizer_tuples never
terminated and wrote temp files until the disk was full (e.g. for
wrapped FASTA, whose lines get paired out of step).

Now throw std::runtime_error if
- an (unweighted) header line does not start with '>',
- a sequence is shorter than k,
- the last sequence line is not terminated by a newline (it used to
  be dropped silently).

test_malformed_input caps RLIMIT_FSIZE, so a regression fails with
SIGXFSZ instead of filling the disk.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A header at the end of the file without a sequence was silently dropped,
and characters such as N were silently encoded as some other base.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A record's sequence now runs until the next '>' header or the end of the
file, so wrapped (multi-line) FASTA is accepted instead of rejected. A
header followed directly by another header is reported as a record with
no sequence. The --check reader in check_from_file.hpp reads records the
same way, so it no longer reports false errors on wrapped input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jermp
jermp merged commit a4ae3cb into jermp:master Nov 28, 2026
16 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.

2 participants