Repository navigation
Description
Description
ObjectToObjectMetricBaseTemplate::NumberOfParametersType is unsigned int. TransformBase::NumberOfParametersType is IdentifierType (64-bit). When a metric uses a dense DisplacementFieldTransform with more than 2^32 parameters, GetNumberOfParameters() truncates the count. ImageToImageMetricv4::InitializeForIteration() then makes the derivative array smaller to the truncated size, and the metric threads write past the end of the buffer. The result is heap corruption (segfault or glibc munmap_chunk(): invalid pointer).
In 3D, the limit is 2^32 / 3 ≈ 1,431,655,765 voxels in the virtual domain. This occurs with SyNImageRegistrationMethod (and the BSpline SyN variants) at full resolution on large images.
Downstream report: ANTsX/ANTs#2033 (852 × 1182 × 1965 = 1,978,880,760 voxels, SyN + ANTS neighborhood CC, ITK 5.4.x).
Call chain (ITK v5.4.7, the version that ANTs uses)
- Type:
itkObjectToObjectMetricBase.h#L173:using NumberOfParametersType = unsigned int;. It is the same onmain. - Truncation:
itkObjectToObjectMetric.hxx#L127-L133returnsm_MovingTransform->GetNumberOfParameters()(64-bit) through the 32-bit return type. There is no check. - Correct allocation in SyN:
itkSyNImageRegistrationMethod.hxx#L595-L597sizesmetricDerivativeasGetNumberOfPixels() * ImageDimension(64-bit). - Buffer made smaller:
itkImageToImageMetricv4.hxx#L289-L292:const NumberOfParametersType globalDerivativeSize = this->GetNumberOfParameters();thenSetSize(globalDerivativeSize). This frees the correct buffer and allocates a smaller one. - Out-of-bounds writes:
itkImageToImageMetricv4GetValueAndDerivativeThreaderBase.hxx#L73aliases each thread'sDerivativesto the smaller buffer.#L315writesDerivatives[offset + i]with a 64-bit offset from the virtual index. - Out-of-bounds read:
itkSyNImageRegistrationMethod.hxx#L633readsmetricDerivative[count++]up topixels * ImageDimension.
Arithmetic
| Virtual domain | Voxels | Parameters (×3) | GetNumberOfParameters() |
|---|---|---|---|
| 568 × 788 × 1310 | 586,335,040 | 1,759,005,120 | correct |
| 852 × 1182 × 1965 | 1,978,880,760 | 5,936,642,280 | 1,641,674,984 |
Same type at other locations
ObjectToObjectMultiMetricv4::GetValueAndDerivative/InitializeForIterationdo the same truncatingSetSize.- The optimizer-weights loop in
SyNImageRegistrationMethod(#L604-L613, only when optimizer weights are set) overruns as well when the size is truncated. - These are 32-bit too, but they do not overflow at these sizes:
ImageVectorOptimizerParametersHelper.hxx#L42(unsigned int sizeInVectors),Array::GetNumberOfElements()(returnsunsigned int), and thevnl_c_vectorhelpers (unsigned n).
Expected behavior
The metric reports the full 64-bit parameter count, or it gives a clear exception when the count is more than the type can hold.
Possible fix
Change ObjectToObjectMetricBaseTemplate::NumberOfParametersType to IdentifierType (the same as TransformBase), and update the cached copies in the metric threaders. If the API change is too large for 5.4.x, add a check in ObjectToObjectMetric::GetNumberOfParameters() that throws when the transform count is more than NumericTraits<NumberOfParametersType>::max().
Versions
ITK v5.4.x (checked at v5.4.0 and v5.4.7) and current main.
The analysis was done with Claude Code and checked against the source.
Activity
BUG: Reject transform parameter counts that exceed the metric limit
BUG: Make cost function and metric parameter counts 64-bit
gdevenyi commentedon Nov 19, 2026
Two follow-ups to the #6991 guard:
main, draft): makesCostFunctionTemplateand the v4 metric parameter counts 64-bit (IdentifierType), so SyN reports the full count for dense fields with more than 2^32 − 1 parameters, instead of throwing. Overrides that returnunsigned intmust change; a migration record covers it.release-5.4, draft): backports the BUG: Reject transform parameter counts that exceed the metric limit #6991 guard, so the 5.4 series throws a clear error instead of corrupting the heap.BUG: Reject transform parameter counts that exceed the metric limit
BUG: Make cost function and metric parameter counts 64-bit