Skip to content

COMP: Address scan-build warnings - #5940

Merged
hjmjohnson merged 17 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:1261-scan-build-warnings
Mar 16, 2026
Merged

hjmjohnson merged 17 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:1261-scan-build-warnings

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Mar 13, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Suppress intentional cplusplus.Move warning in itkArray2DGTest.cxx move-semantics tests that deliberately verify moved-from object state
  • Fix cplusplus.NewDelete zero-sized allocation in itkQuadEdgeMeshCellInterfaceTest.cxx by guarding with numberOfPoints > 0
  • cplusplus.NewDelete findings in itkSmartPointer.h are confirmed false positives (refcount prevents use-after-free); no code change needed

Test plan

  • ITKCommonGTestDriver builds and all Array2D.* tests pass
  • ITKQuadEdgeMeshTestDriver builds and itkQuadEdgeMeshCellInterfaceTest passes
  • CI passes

Addresses #1261 (scan-build report comment)

🤖 Generated with Claude Code

@github-actions github-actions Bot added type:Compiler Compiler support or related warnings type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:IO Issues affecting the IO module labels Mar 13, 2026
@hjmjohnson
hjmjohnson force-pushed the 1261-scan-build-warnings branch 2 times, most recently from 9527b5a to 0525edc Compare March 13, 2026 14:23
@github-actions github-actions Bot added area:Numerics Issues affecting the Numerics module area:Filtering Issues affecting the Filtering module area:Segmentation Issues affecting the Segmentation module labels Mar 13, 2026
@hjmjohnson
hjmjohnson force-pushed the 1261-scan-build-warnings branch from cb6f4eb to c68b2c8 Compare March 13, 2026 16:21
Comment thread Modules/IO/MeshOFF/src/itkOFFMeshIO.cxx Resolved
@hjmjohnson
hjmjohnson force-pushed the 1261-scan-build-warnings branch 4 times, most recently from 5f1ace3 to 356b689 Compare March 13, 2026 18:25
@hjmjohnson
hjmjohnson marked this pull request as ready for review March 13, 2026 19:05
@hjmjohnson
hjmjohnson requested a review from dzenanz March 13, 2026 19:55
@hjmjohnson hjmjohnson changed the title COMP: Address cplusplus.Move and cplusplus.NewDelete scan-build warnings COMP: Address scan-build warnings Mar 13, 2026
@hjmjohnson
hjmjohnson force-pushed the 1261-scan-build-warnings branch from 356b689 to c50738e Compare March 13, 2026 22:32
@github-actions github-actions Bot added type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots area:Video Issues affecting the Video module labels Mar 13, 2026

@dzenanz dzenanz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mostly looks good.

{
itkExceptionMacro("TIFF palette images require at most 16 bits per sample, but got " << bps);
}
const tmsize_t array_size = (tmsize_t{ 1 } << bps) * sizeof(uint16_t);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The commit message sounds outrageous. Is this correct? @issakomi @malaterre

hjmjohnson and others added 6 commits March 13, 2026 18:46
  1. Silences the cplusplus.Move checker for these intentional accesses and silences the corresponding clang-tidy bugprone-use-after-move check
  2. Keeps the valuable test logic that verifies move semantics are implemented properly
When TestQECellInterface is called with a 0-vertex QEPolygonCellType,
`new PointIdentifier[0]` produces a zero-sized heap allocation. While
technically valid C++, any dereference would be undefined behavior.
The clang static analyzer (cplusplus.NewDelete) correctly flags this
as "Use of memory allocated with size zero."

Wrap the allocation, population, and SetPointIds/iterator exercise
block in a `numberOfPoints > 0` guard. The 0-vertex case still
exercises the empty-cell iterator paths earlier in the function.

Addresses cplusplus.NewDelete finding from issue InsightSoftwareConsortium#1261.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The base class ReadHeader returns nullptr, and subclass overrides may
also return null without throwing. Two call sites in ReadImageInformation
dereference the result without null checks:

1. m_ImageHeader->modality (line 140) — null dereference when ReadHeader
   fails silently. Now throws itkExceptionMacro on null return.
2. curImageHeader->examNumber (line 223) — null dereference in the
   directory scan loop. Now skips null results with continue.

Addresses nullability.NullPassedToNonnull and core.NullDereference
findings from issue InsightSoftwareConsortium#1261.
The Singleton<T>() template used `new T` (default-initialization) which
leaves POD types like bool and unsigned int with indeterminate values.
While the itkGetGlobalInitializeMacro immediately assigns the intended
value on first creation, the analyzer cannot trace through the singleton
index indirection and correctly flags the intermediate indeterminate
state.

Change to `new T{}` (value-initialization) which zero-initializes POD
types, eliminating undefined behavior and resolving:
- core.uninitialized.UndefReturn in itkDataObject.cxx:142
- core.uninitialized.UndefReturn in itkObject.cxx:452
- core.uninitialized.UndefReturn in itkMetaImageIO.cxx:940
- core.UndefinedBinaryOperatorResult in itkDataObject.cxx:131

Addresses scan-build findings from issue InsightSoftwareConsortium#1261.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
AllocateTiffPalette had two bugs on the array_size computation:

1. Operator precedence: `tmsize_t{1} << bps * sizeof(uint16_t)` was
   parsed as `1 << (bps * 2)` instead of `(1 << bps) * sizeof(uint16_t)`.
   For bps=8 this over-allocated 128x; for bps=16 it attempted ~4GB.

2. Undefined behavior: with bps=32 (float), the shift amount was 64,
   which is UB for a 64-bit type. The TIFF spec limits palette images
   to at most 16 bits per sample but this was not enforced.

Add a bps > 16 guard (with itkExceptionMacro) and fix parentheses
to compute the intended allocation size.

Addresses core.BitwiseShift finding from issue InsightSoftwareConsortium#1261.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…test

Cache GetNumberOfPoints() result and value-initialize dynamically
allocated PointIdentifier arrays to fix core.CallAndMessage warnings
from clang static analyzer about uninitialized values passed to
SetPointIds/InternalSetPointIds.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
hjmjohnson and others added 11 commits March 13, 2026 18:46
Move the `numberOfElements == 0` check before the size-mismatch
check in CalculateFitError to help the clang static analyzer
verify the division-by-zero guard on line 55.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Remove three dead store patterns flagged by clang scan-build:
- itkPoolMultiThreader.cxx: discard unused return from GetSplit()
  after the work-unit loop (side effect on iRegion is what matters)
- itkCSVFileReaderBase.cxx: remove trailing OnANewLine=false at end
  of scope where the local is never read again
- itkGiftiMeshIO.cxx: remove assignments to compType/pixelType in
  switch default cases that unconditionally throw

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Use `if (testStatus != EXIT_SUCCESS)` to ensure test failures are correctly detected and reported.
- Replace dead stores and improve the consistency of test status handling by using bitwise OR (`|=`) for cumulative test statuses.
- Add failure return conditions for helper functions to avoid unreported test failures.
- Replace uninitialized variables with `const` initialization for clarity and safety against potential unintended usage.
The second verification loop used a hardcoded bound of 10, which
exceeds NumberOfSteps() for small Hilbert orders (e.g., order=1 in
2D has only 4 steps). Replace with path->NumberOfSteps() to match
the actual path size.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The helper function had two pre-existing bugs exposed by the |=
accumulation fix:

1. The FloatAlmostEqual check was inverted (set pass=false when
   values ARE equal). Since ULP comparison never matched the
   optimizer's approximate convergence, this was effectively dead
   code. Replace with std::abs tolerance check (1e-2).

2. The GetCurrentIteration()>0 check was intended only for the
   zero-iterations case but ran for all calls. Guard it with a
   numberOfIterations==0 condition.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Eliminated a dead store in `itkLevelSetDenseImageTest.cxx`.
- Updated `theoreticalValue` and `value` variables to use `const` for clarity and safety.
- Eliminated duplicate statements for `optimizerInitialRadius` and `filter->SetOptimizerInitialRadius` to improve clarity and prevent unnecessary operations.
- Removed unnecessary `start = &x[0]` assignment to improve test code clarity and avoid dead store.
- Replace repetitive try-catch blocks with `ITK_TRY_EXPECT_EXCEPTION` macros for improved clarity and maintainability.
Replace raw `new PointIdentifier[]` with `std::vector<PointIdentifier>`
and `std::iota` in TestCellInterface and TestQECellInterface. This
resolves core.CallAndMessage scan-build findings where the analyzer
could not verify value-initialization of raw new[] arrays.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Warns if std::move is applied to a forwarding reference (a T&& parameter
where T is a deduced template type). In such cases std::forward should be
used instead, to preserve the value category of the argument.
@hjmjohnson
hjmjohnson force-pushed the 1261-scan-build-warnings branch from c50738e to 14c265b Compare March 13, 2026 23:48
@github-actions github-actions Bot removed the area:Video Issues affecting the Video module label Mar 13, 2026
@hjmjohnson

Copy link
Copy Markdown
Member Author

Arm failures fixed in previously merged PR.

@hjmjohnson
hjmjohnson merged commit 13abcb9 into InsightSoftwareConsortium:main Mar 16, 2026
5 checks passed
@hjmjohnson hjmjohnson added this to the ITK 6.0 Release Candidate 1 milestone May 5, 2026
hjmjohnson added a commit to hjmjohnson/ITK that referenced this pull request May 6, 2026
…1-scan-build-warnings

COMP: Address scan-build warnings
hjmjohnson added a commit to hjmjohnson/ITK that referenced this pull request May 12, 2026
…1-scan-build-warnings

COMP: Address scan-build warnings
@hjmjohnson
hjmjohnson deleted the 1261-scan-build-warnings branch August 22, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Numerics Issues affecting the Numerics module area:Segmentation Issues affecting the Segmentation module type:Compiler Compiler support or related warnings type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants