Skip to content

Check for null when calling TiffParser.checkHeader()#4429

Open
melissalinkert wants to merge 1 commit into
ome:developfrom
melissalinkert:tiff-checkheader-null
Open

Check for null when calling TiffParser.checkHeader()#4429
melissalinkert wants to merge 1 commit into
ome:developfrom
melissalinkert:tiff-checkheader-null

Conversation

@melissalinkert
Copy link
Copy Markdown
Member

Fixes #4232.

Most calls to checkHeader already check for a null return value, or follow a call to isValidHeader which checks for null itself. In the few remaining cases here, the changes are mostly throwing a more informative exception, or logging an error when that isn't reasonable. checkHeader returning null implies that the TIFF file is invalid, so throwing an exception of some kind is usually appropriate.

This should not be a breaking change, and is not expected to have any impact on tests.

@melissalinkert melissalinkert added this to the 9.0.0 milestone May 13, 2026
@melissalinkert melissalinkert requested a review from sbesson May 13, 2026 18:22
Copy link
Copy Markdown
Member

@sbesson sbesson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed changes are consistent with other readers like FlowSightReader.java or MinimalTiffReader which check the output of TiffParser.checkHeader and throw a FormatException if the return value is null.

To assess the functional impact on the readers, I constructed a synthetic Metamorph dataset derived from a public sample, replacing individual TIFF files by empty files. When the first TIFF file in the list (test_timelapse_20240816_s1_t1.TIF) is corrupted, using both Bio-Formats 8.5.0 or this PR, the reader initialization would fail on the file with:

loci.formats.FormatException: Invalid TIFF file: /Users/sbesson/Downloads/Bio-Formats_testdata/test_timelapse_20240816_s1_t1.TIF
	at loci.formats.in.MinimalTiffReader.initFile(MinimalTiffReader.java:452)
	at loci.formats.in.BaseTiffReader.initFile(BaseTiffReader.java:609)
	at loci.formats.in.MetamorphReader.initFile(MetamorphReader.java:425)
	at loci.formats.FormatReader.setId(FormatReader.java:1480)
	at loci.formats.ImageReader.setId(ImageReader.java:864)
	at loci.formats.ReaderWrapper.setId(ReaderWrapper.java:692)
	at loci.formats.tools.ImageInfo.testRead(ImageInfo.java:1054)
	at loci.formats.tools.ImageInfo.main(ImageInfo.java:1165)

When corrupting another file e.g. test_timelapse_20240816_s1_t2.TIF, the latest release of Bio-Formats 8.5.0 initializes the dataset, does not includes test_timelapse_20240816_s1_t2.TIF in the used files list and renders the corresponding plane as empty. With this change, the reader fails at initialization time with:

loci.formats.FormatException: Invalid TIFF file: /Users/sbesson/Downloads/Bio-Formats_testdata/test_timelapse_20240816_s1_t2.TIF
	at loci.formats.in.MetamorphReader.initFile(MetamorphReader.java:1175)
	at loci.formats.FormatReader.setId(FormatReader.java:1480)
	at loci.formats.ImageReader.setId(ImageReader.java:864)
	at loci.formats.ReaderWrapper.setId(ReaderWrapper.java:692)
	at loci.formats.tools.ImageInfo.testRead(ImageInfo.java:1054)
	at loci.formats.tools.ImageInfo.main(ImageInfo.java:1165)

I will look into constructing similar examples for LeicaReader and ZeissLSMReader.
Based on the above, the outstanding question is whether increasing the strictness in terms of file validity is part of the contract/expectation of this PR or whether we want to retain the leniency of the current implementation.

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.

NULL being returned for a Boolean object

2 participants