Opened 14 years ago
Closed 13 years ago
#2752 closed Bugs (fixed)
Wrong size check in bounded_array::resize
Reported by: | Owned by: | Gunter | |
---|---|---|---|
Milestone: | Boost 1.39.0 | Component: | uBLAS |
Version: | Boost 1.38.0 | Severity: | Problem |
Keywords: | Cc: |
Description
The resize methods in the bounded_array class, in the file boost/numeric/ublas/storage.hpp, are defined as
// Resizing BOOST_UBLAS_INLINE void resize (size_type size) { BOOST_UBLAS_CHECK (size_ <= N, bad_size ()); size_ = size; } BOOST_UBLAS_INLINE void resize (size_type size, value_type init) { BOOST_UBLAS_CHECK (size_ <= N, bad_size ()); if (size > size_) std::fill (data_ + size_, data_ + size, init); size_ = size; }
Shouldn't the BOOST_UBLAS_CHECKs test for size < N? As the checks are written now, it is possible to assign a new size that is larger than N.
Ulf
Change History (3)
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Say that you have have created a bounded_array with three elements (N=3 and size_=3). Then you, by mistake, try to resize that bounded_array to hold four elements. That is, your code is
#include <boost/numeric/ublas/storage.hpp> int main () { using namespace boost::numeric::ublas; bounded_array<double, 3> a (3); a.resize(4); // Should result in an exception, but it doesn't! }
The solution for the definition of the resize function is either to check the function argument, size, instead of the member size_, or to first do the assignment statement and then the check.
How can the size be larger than N?
It should be okay to set the size equal to N, because there is storage for N elements, right? If size is greater than N, then it will be caught by the BOOST_UBLAS_CHECK.
What am I missing?