Opened 10 years ago

Last modified 10 years ago

#7680 new Bugs

Poor choice of difference_type for zip_iterator

Reported by: matthias_berndt@… Owned by: jeffrey.hellrung
Milestone: To Be Determined Component: iterator
Version: Boost 1.52.0 Severity: Problem
Keywords: Cc:

Description

Hi,

the following program results in an integer overflow on my system (Linux g++ 4.7.2):

#include <boost/iterator/zip_iterator.hpp>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <iostream>
int main() {
  boost::counting_iterator<unsigned short> i1(0);
  boost::counting_iterator<unsigned long long> i2(0);
  auto i3 = boost::make_zip_iterator(boost::make_tuple(i1, i2));
  i3 += 3221225472u;
  std::cout << boost::get<1>(*i3) << '\n';
}

It prints 18446744072635809792, but I'd expect it to print 3221225472. boost::zip_iterator seems to use the first underlying iterator's difference_type as its own difference_type, which isn't necessarily the right choice.

Change History (2)

comment:1 by anonymous, 10 years ago

According to the iterator concepts, the difference_type needs to be a signed integral type. It should thus easily be possible to choose the difference_type with the biggest range from the underlying iterator types.

Also note that g++-4.7.2 emits a warning for the above code when compiled with -Wsign-conversion.

comment:2 by Dave Abrahams, 10 years ago

Owner: changed from Dave Abrahams to jeffrey.hellrung
Note: See TracTickets for help on using tickets.