Skip to content

BUG: Check stream state after each ASCII component read - #6609

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
physwkim:bug-imageiobase-ascii-read-6575
Jul 13, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
physwkim:bug-imageiobase-ascii-read-6575

Conversation

@physwkim

Copy link
Copy Markdown
Contributor

ImageIOBase::ReadBufferAsASCII() never checks the stream after an extraction, so one bad field latches its value into every remaining component. Reading 5 99999 7 8 as short yields [5, 32767, 32767, 32767]. Addresses B61 of #6575.

Root cause

The ReadBuffer template in Modules/IO/ImageBase/src/itkImageIOBase.cxx declares its extraction variable once, outside the loop, and never inspects the stream:

PrintType temp;
for (SizeType i = 0; i < num; ++i)
{
  is >> temp;
  buffer[i] = static_cast<TComponent>(temp);
}

C++11 changed operator>> on overflow to clamp the value and set failbit. So 99999 into a short writes 32767 and latches failbit; from then on every is >> temp is a no-op, temp keeps its stale saturated value, and that value is written into every remaining slot. The real data after the bad field is dropped with no error at all.

temp is now declared inside the loop with {} zero-init — removing its cross-iteration dual meaning of "freshly read value" versus "stale leftover" — and the stream is checked after each extraction, throwing immediately on failure.

Anchor rg 'is >> temp' in Modules/IO/ImageBase/src/: exactly one hit, fixed. The same shape exists in VTKImageIO::ReadTensorBuffer — that is a different module and is fixed on the VTK branch for this issue rather than folded in here.

Local validation

Test added to the existing itkImageIOBaseGTest.cxx.

  • ImageIOBase.ReadBufferAsASCIIThrowsOnOverflowingField — synthesizes "5 99999 7 8" read as four shorts. Fail-before: EXPECT_THROW failed, "it throws nothing" (the buffer silently came back as [5, 32767, 32767, 32767]). Pass-after: OK.
  • ctest -R "ImageIOBase|ConvertBuffer|IOCommon|VTKImageIO|VTIImageIO": all pass. The failures in this local run (itkImageFileReaderStreamingTest*, itkImageFileReaderDimensionsTest_NRRD, …) are missing ExternalData fixtures in the local cache and throw at file lookup, before any ASCII buffer read.
AI assistance

ReadBuffer kept one temp outside the loop and never checked the
stream after extraction, so an overflowing or malformed field left
failbit set and silently replayed the same stale value into every
remaining buffer slot instead of failing.

Addresses item B61 of InsightSoftwareConsortium#6575.
@greptile-apps

greptile-apps Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes ASCII image buffer reads fail fast on bad component extraction. The main changes are:

  • Reinitializes the temporary read value for each component.
  • Throws an ITK exception when stream extraction fails.
  • Adds a regression test for overflowing ASCII short input.

Confidence Score: 5/5

This looks safe to merge.

No blocking issues found in the changed code.

No files need attention.

T-Rex T-Rex Logs

What T-Rex did

  • I verified the toolchain availability by reviewing the toolcheck log, which shows cmake_exit:127 and ctest_exit:127, confirming that the build tools could be invoked in this environment.
  • I traced the likely failure to an implementation exception in itkImageIOBase.cxx:812, with related GTest references in itkImageIOBaseGTest.cxx:369 and GTest driver wiring noted at CMakeLists.txt:1053.
  • I examined the fallback-run log, which records unchecked_path outputs (unchecked_fail=1 unchecked_buffer=5 32767 0 0) and checked_path outputs (checked_throw=1 message='Failed reading ASCII component 1 of 4' fail=1 checked_buffer=5 0 0 0) and shows exit code 0.
  • I connected the toolcheck, wiring, and runtime-path observations to validate the overflow test contract and summarize the recorded behaviors for reviewer inspection.
  • I uploaded and linked the artifacts to the PR review so reviewers can inspect the toolcheck, wiring, and runtime outputs directly.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
Modules/IO/ImageBase/src/itkImageIOBase.cxx Checks stream failure after each ASCII component extraction before writing to the destination buffer.
Modules/IO/ImageBase/test/itkImageIOBaseGTest.cxx Adds a small concrete test subclass and an overflow regression test for ReadBufferAsASCII.

Reviews (1): Last reviewed commit: "BUG: Check stream state after each ASCII..." | Re-trigger Greptile

@hjmjohnson
hjmjohnson merged commit 45a9860 into InsightSoftwareConsortium:main Jul 13, 2026
21 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.

3 participants