Skip to content

BUG: Reject invalid grid header values in the CTF and CPR readers - #76

Closed
imikejackson wants to merge 1 commit into
BlueQuartzSoftware:developfrom
imikejackson:fix/hkl-grid-header-validation
Closed

imikejackson wants to merge 1 commit into
BlueQuartzSoftware:developfrom
imikejackson:fix/hkl-grid-header-validation

Conversation

@imikejackson

Copy link
Copy Markdown
Collaborator

Summary

ModifiedLambertProjection did not check its dimension.

  • initializeSquares(0, r) set m_StepSize = squareEdge / 0 (infinite) and allocated empty squares. LambertBallToSquare() then computed NaN bins and wrote outside the squares: segfault.
  • initializeSquares(-1, r) kept dimension -1 and a negative step size, and converted -1 to size_t for the square size (SIZE_MAX x SIZE_MAX wraps to 1 bin). Other negative values ask for huge allocations.
  • createStereographicProjection(-1) / createCircularProjection(-1) returned a 1-pixel image for the same reason.
  • Pole figure entry points: ComputeStereographicProjection (used by every LaueOps::generatePoleFigure()) wrote into an empty intensity image for imageDim == 0 in discrete mode (segfault). For imageDim == 1, halfDim - 1 is -1, so equator points were written one pixel before or after the 1-pixel image. With lambertDim == 0 the continuous path crashed in the Lambert projection. InversePoleFigureUtilities::computeIPFIntensity() iterated lambertDim * lambertDim bins regardless of what was allocated.
  • The xpoints / 2 and 2.0f / xpoints expressions in the stereographic projection are harmless on their own: when xpoints is 0 the pixel loops do not run.

Design

The class has no error channel (initializeSquares() returns void, LambertBallToSquare() returns a pointer that no caller null-checks). The least invasive fix that keeps every public signature:

  • initializeSquares() rejects a dimension of 0 or less by leaving the projection empty: getDimension() and getStepSize() are 0 and both squares are non-null arrays with 0 bins. This is documented on initializeSquares() and LambertBallToSquare(); callers detect it with getDimension() > 0.
  • addInterpolatedValues() does nothing and getInterpolatedValue() returns 0 on an empty projection, so LambertBallToSquare() and both stereographic projections return all-0 images instead of crashing. Returning NullPointer() from LambertBallToSquare() was rejected because eight existing call sites dereference the result without a check.
  • createStereographicProjection(int) and createCircularProjection() clamp a negative image dimension to 0.
  • ComputeStereographicProjection returns an empty image for imageDim <= 0, and the discrete path skips points outside the image. For imageDim >= 2 every point lands inside, so valid output is unchanged.
  • computeIPFIntensity() counts the bins the projection allocated.
  • No error codes are added; valid dimensions behave as before.

Test Plan

  • New tests in ModifiedLambertProjectionTest.cpp fail on develop and pass with this change:
    • InitializeSquares_NonPositiveDimension: on develop CHECK( mlp->getStepSize() == 0.0f ) fails with inff == 0.0f, then segfaults interpolating.
    • LambertBallToSquare_ZeroDimension: on develop SIGSEGV.
    • CreateProjection_NegativeDimension: on develop stereo->getNumberOfTuples() == 0 fails with 1 == 0 (size_t wrap).
    • ComputeStereographicProjection_SmallDimensions (discrete imageDim 0 and 1, lambertDim 0): on develop SIGSEGV.
    • ComputeIPFIntensity_ZeroLambertDimension: on develop SIGSEGV.
    • No test allocates a large array; -1 is used because its wrap is 1 bin.
  • Linux x64, Release: full ctest suite passes, FULL_COUNT.

Related Issue

Related to BlueQuartzSoftware/simplnx#1908 (item L90) and BlueQuartzSoftware/simplnx#1957. simplnx #1965 already range-checks the pole figure dimensions in preflight; this change makes EbsdLib safe for other callers too.

Overlaps: open PR #71 changes the pole-figure compositor (PoleFigureCompositor.cpp), which this PR does not touch; no file overlap and no merge-order requirement.

The bluequartz-skills skills (cpp-best-practices, simplnx-best-practices) could not be loaded in this environment; the repository's CLAUDE.md coding standards were followed.

🤖 Generated with Claude Code

- Before: CtfReader::readFile() and CprReader::readFile() only rejected a step
  or cell count of exactly zero. Negative, NaN and infinite steps were accepted
  (a 3D .ctf file skips the raster-order check that otherwise catches them),
  negative .cpr xCells/yCells were accepted (-1 x -1 read as a 1 point grid),
  and a .ctf "ZCells 0" header allocated zero-length arrays that the raster
  order check then read past.
- After: non-finite or negative steps and non-positive ZCells (.ctf) or
  negative xCells/yCells (.cpr) return new negative error codes
  (-102310..-102312 for .ctf, -102320..-102323 for .cpr) with a message that
  names the file, the header key and the value. Zero values keep their
  existing codes (-102, -103) and a .ctf file without a ZCells line is still
  read as one slice.
- Add HklGridHeaderValidationTest with small synthetic .ctf and .cpr files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@imikejackson imikejackson mentioned this pull request Nov 13, 2026
7 tasks done
@imikejackson

Copy link
Copy Markdown
Collaborator Author

Closed in favor of #86, which merges this branch (with its history) together with the other open EbsdLib fixes for the next release, so simplnx can build against one EbsdLib branch. The branch is kept.

🤖 Generated with Claude Code

@imikejackson
imikejackson deleted the fix/hkl-grid-header-validation branch November 13, 2026 16:28
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