Skip to content

Fix SonarCloud blocker and critical findings in biojava-core - #1177

Merged
josemduarte merged 9 commits into
biojava:masterfrom
josemduarte:jd/sq-core
Dec 5, 2026
Merged

josemduarte merged 9 commits into
biojava:masterfrom
josemduarte:jd/sq-core

Conversation

@josemduarte

@josemduarte josemduarte commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Works through the SonarCloud blocker findings in biojava-core, and the critical findings that point at possible bugs.

Bugs fixed (each with a test that failed first)

  • GeneSequence.getSequence5PrimeTo3Prime(), negative strand: returned the sequence reversed but not complemented. getSequenceAsString(begin, end, NEGATIVE) already gives the reverse complement, and the method complemented it again. This is the bug o.b.n.core.sequence.CDSSequence doesn't reverse-complement correctly #1016 fixed in CDSSequence; GeneSequence was missed. FastaGeneWriter (its only caller) wrote such genes that way, and FastaGeneWriterTest expected that output; the expectation is corrected.
  • SearchIO shared one parser across readers: it cached one ResultFactory instance per extension and gave it to every SearchIO. Parsers keep per-file state, so a headerless BLAST tabular file read after one with a header reported the first file's program name. The cache was also published before it was filled. It now caches classes and creates a parser per SearchIO.
  • BLAST tabular program name: kept its # prefix (replace was given a regex), and the # BLAST processed N queries trailer overwrote it.
  • XXE: XMLHelper.inputStreamToDocument() (and so loadXML() and the BLAST XML parser) enabled its secure-processing feature after building the parser, which has no effect and doesn't block external entities anyway. It now uses the hardened factory, and the Transformer no longer fetches external DTDs or stylesheets.
  • InputStreamProvider: for .zip/.jar, closing the returned stream didn't close the archive, which leaked one file descriptor per read. A new test checks the open file descriptor count. Failed GZIP/.Z wrapping now closes the underlying stream.

Other fixes

  • Malformed input gives a clear error instead of ArrayIndexOutOfBoundsException: short BLAST tabular rows throw ParseException, short ABI data throws the documented IllegalArgumentException, and a malformed AAindex matrix header throws IOException.
  • Resource handling: try-with-resources in ClasspathResource and IOUtils.print, which now also reports write errors as its javadoc says. The BLAST tabular parser now closes its Scanner.
  • StringManipulationHelper.equalsToXml: marked @Deprecated. It is documented as unimplemented and always throws.
  • Tests: assertion-less tests now check something real. Two empty placeholders and a test that only called a setter with null are removed.
  • Cleanups: Integer.compare in the AbstractFeature comparators, default last in two switches, replace for literal patterns, and explicit defaults in FastaReaderTest.

Not addressed

  • AbstractFeature.TYPE (S1845): a public constant, so renaming it breaks the API.
  • FileDownloadUtils temp file (S5443): a false positive. Files.createTempFile creates an owner-only file with a random name.
  • equalsToXml (S3516): stays flagged until 8.0 removes it with the other deprecated APIs (BioJava 8.0 plan: Java 21, a leaner, library-only BioJava #1168 item 7).
  • Style and refactoring criticals (~107): empty methods, method complexity, constant names and serializable fields.

Testing

  • mvn test on every module except biojava-integrationtest passes locally (about 1,450 tests, 0 failures).

🤖 Generated with Claude Code

josemduarte and others added 9 commits December 4, 2026 19:53
XMLHelper.inputStreamToDocument(), used by loadXML() and so by the BLAST
XML parser, enabled FEATURE_SECURE_PROCESSING only after creating its
DocumentBuilder, which has no effect, and that feature does not block
external entities anyway: a crafted file could read local files (Sonar
java:S2755, blocker). Use newSecureDocumentBuilderFactory() instead.

Also stop the Transformer in outputToStream() from fetching external
DTDs or stylesheets, and use the secure factory in
StringManipulationHelper.equalsToXml(). That method is documented as not
implemented and always throws, so mark it @deprecated for removal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- InputStreamProvider: for .zip and .jar files the stream to the first
  entry was returned without any way to close the ZipFile/JarFile behind
  it, so each open leaked a file descriptor until garbage collection,
  and an archive with no entries leaked before throwing (Sonar
  java:S2095, blocker). Return a stream that also closes the archive.
  When a GZIP or .Z wrapper fails to open, close the underlying stream.
  New tests, including one that checks the open file descriptor count.
- ClasspathResource: try-with-resources for the pre-cache copy, and
  close the stream if it turns out not to be GZIP (S2093).
- IOUtils.print: try-with-resources, and write through a plain Writer so
  write errors are reported as the javadoc says, rather than swallowed
  by PrintWriter (S2093).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The program name from a "# TBLASTN 2.2.29+" header kept its "# "
  prefix because String.replace() takes a literal, not the regex
  "#\s" it was given. Use replaceFirst.
- A row with fewer than the 12 standard columns threw
  ArrayIndexOutOfBoundsException (Sonar javabugs:S6466); throw the
  ParseException that createObjects() already declares.
- createObjects() never closed its Scanner; use try-with-resources.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ABITrace documents an IllegalArgumentException for data that is not a
valid ABI file, but isABI() read bytes 0-2 and 128-130 without checking
the length, so short data threw ArrayIndexOutOfBoundsException (Sonar
javabugs:S6466).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
initMatrix() read fields 3 and 6 of the "M rows = ..., cols = ..." line
without checking they exist, so a malformed header threw
ArrayIndexOutOfBoundsException out of parse() (Sonar javabugs:S6466).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SearchIO cached one ResultFactory instance per file extension in a
static map and handed that same instance to every SearchIO. Parsers keep
per-file state, so reading a headerless BLAST tabular file after one
with a header reported the first file's program name. The cache was also
published before it was filled, so another thread could see it half
empty (Sonar java:S2696).

Cache the factory class per extension instead, loaded once through a
holder class, and create a new factory for each SearchIO.

Also stop the "# BLAST processed N queries" trailer from overwriting the
program name: only accept a header line with a version after the name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Behaviour-preserving cleanups:

- AbstractFeature comparators: compare the integer positions with
  Integer.compare instead of widening them to double (S9148, S3973).
- SimpleProfile and MultipleSequenceAlignment.toString(StringFormat):
  move the CLUSTAL/default group to the end of the switch (S4524).
- replace() instead of replaceAll() where the pattern is a literal, in
  MultipleSequenceAlignment, GenbankSequenceParser,
  GenericInsdcHeaderFormat and FileDownloadUtils (S5361).
- FastaReaderTest: explicit default cases in switches that only check
  selected sequences (S131).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For a negative-strand gene, getSequenceAsString(begin, end, NEGATIVE)
already returns the reverse complement, and the method then complemented
it again, returning the forward sequence reversed. This is the bug fixed
in CDSSequence.getCodingSequence by biojava#1016, still present in GeneSequence.
FastaGeneWriter, its only caller, wrote such genes reversed but not
complemented.

Found by replacing an assertion-less test (Sonar java:S2699) with a real
check. FastaGeneWriterTest expected the old output; its expectation is
corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
These tests only checked that nothing threw (Sonar java:S2699, blocker):

- BlastXMLParserTest: parsing without a file throws
  IllegalStateException; storeObjects() throws the documented
  UnsupportedOperationException.
- BlastTabularParserTest.testSetFile only called a setter with null and
  had nothing to check; setFile is covered by testCreateObjects. Removed.
- ChromosomeSequenceTest: removing a missing gene leaves no genes; two
  empty placeholder tests removed (addGene/getGene are covered above).
- TranslationTest: table 1 and "UNIVERSAL" are the same table.
- GenbankCookbookTest: the local GenBank files give non-empty, identical
  results through GenbankReaderHelper and GenbankReader; the NCBI part
  checks that an accession was parsed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@josemduarte
josemduarte merged commit b26fc3d into biojava:master Dec 5, 2026
5 checks passed
josemduarte added a commit that referenced this pull request Dec 5, 2026
It was disabled with "seems to complement twice???", had no assertion and
only printed the result. That double complement was fixed in #1177, and
negativeStrandSequence5PrimeTo3PrimeIsReverseComplement covers it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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