Skip to content

Commit

Permalink
COMP: Fix bit shift overflow warning
Browse files Browse the repository at this point in the history
itkStatisticsLabelMapFilter.hxx(77): warning C4293: '<<': shift count
negative or too big, undefined behavior
  • Loading branch information
blowekamp committed Nov 5, 2021
1 parent a8c4c60 commit 0501e41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ class ITK_TEMPLATE_EXPORT StatisticsLabelMapFilter
static constexpr unsigned int
GetDefaultNumberOfBins()
{
return NumericTraits<FeatureImagePixelType>::IsInteger && sizeof(FeatureImagePixelType) <= 2
? 1 << (8 * sizeof(FeatureImagePixelType))
: 128;
constexpr size_t bitsShift = std::min(8 * sizeof(FeatureImagePixelType), 8 * sizeof(m_NumberOfBins) - 1);

return std::is_integral<FeatureImagePixelType>::value && sizeof(FeatureImagePixelType) <= 2 ? 1u << bitsShift
: 128u;
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ StatisticsLabelMapFilter<TImage, TFeatureImage>::ThreadedProcessLabelObject(Labe
typename HistogramType::MeasurementVectorType featureImageMax(1);


if (NumericTraits<typename Self::FeatureImagePixelType>::IsInteger &&
m_NumberOfBins == 1 << (8 * sizeof(typename Self::FeatureImagePixelType)))
constexpr size_t bitsShift = std::min(8 * sizeof(FeatureImagePixelType), 8 * sizeof(m_NumberOfBins) - 1);
if (std::is_integral<FeatureImagePixelType>::value && sizeof(FeatureImagePixelType) <= 2 &&
m_NumberOfBins == 1 << bitsShift)
{
// Add padding so the center of bins are integers
featureImageMin.Fill(NumericTraits<typename Self::FeatureImagePixelType>::min() - 0.5);
Expand Down

0 comments on commit 0501e41

Please sign in to comment.