From e612e423e55103c9bf5932476e7f1d7ee1450f10 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Fri, 12 Sep 2014 09:51:07 -0600 Subject: [PATCH] Check for wide iterators in ArrayPortalFromIterators. When compiling with 32-bit Ids for a 64 bit machine (which is not uncommon), it is possible that the distance between two iterators is larger than the maximum value that can be stored in vtkm::Id. If two such iterators were passed to ArrayPortalFromIterators, that would cause problems. This change checks for that condition and throws an out of memory exception if it occurs. That would be a pretty darn big array and is more likely to be the cause of an error somewhere else in the code, but either way the check and error is good. This change also fixes a warning we have been getting with MSVC. --- vtkm/cont/internal/ArrayPortalFromIterators.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/vtkm/cont/internal/ArrayPortalFromIterators.h b/vtkm/cont/internal/ArrayPortalFromIterators.h index 3846c1041..88679dc13 100644 --- a/vtkm/cont/internal/ArrayPortalFromIterators.h +++ b/vtkm/cont/internal/ArrayPortalFromIterators.h @@ -24,8 +24,10 @@ #include #include #include +#include #include +#include namespace vtkm { namespace cont { @@ -44,9 +46,20 @@ public: VTKM_CONT_EXPORT ArrayPortalFromIterators(IteratorT begin, IteratorT end) - : BeginIterator(begin), NumberOfValues(std::distance(begin, end)) + : BeginIterator(begin) { - VTKM_ASSERT_CONT(this->GetNumberOfValues() >= 0); + typename std::iterator_traits::difference_type numberOfValues = + std::distance(begin, end); + VTKM_ASSERT_CONT(numberOfValues >= 0); +#ifndef VTKM_USE_64BIT_IDS + if (numberOfValues > std::numeric_limits::max()) + { + throw vtkm::cont::ErrorControlOutOfMemory( + "Distance of iterators larger than maximum array size." + "To support larger arrays, try 64 bit arrays."); + } +#endif // !VTKM_USE_64BIT_IDS + this->NumberOfValues = static_cast(numberOfValues); } /// Copy constructor for any other ArrayPortalFromIterators with an iterator