Opened 14 years ago

Closed 12 years ago

#2428 closed Bugs (wontfix)

[random] missing member initialization of generator_iterator ctor

Reported by: Johannes Brunen <JBrunen@…> Owned by: No-Maintainer
Milestone: Boost 1.37.0 Component: random
Version: Boost 1.36.0 Severity: Problem
Keywords: generator_iterator default ctor Cc: JBrunen@…, dwalker07@…

Description

I am trying to use boost::generator_iterator with a random number generator as follows:

--- #include <boost/generator_iterator.hpp> #include <boost/random/mersenne_twister.hpp>

class my_generator {...};

my_generator gen;

boost::generator_iterator<my_generator> first(&gen); boost::generator_iterator<my_generator> last;

boost::random::mt19937 rng; rng.seed(first, last);

---

Sadly the default constructor of generator_iterator leaves m_gen uninitialized and the seed function is trying to compare first with last.

This yield undefined behavior.

Solution: default ctor member initialization:

generator_iterator.hpp : line 38

generator_iterator() : m_g(0), m_value(0) {}

Attachments (1)

generator_iterator.hpp (1.9 KB ) - added by Johannes Brunen <JBrunen@…> 14 years ago.

Download all attachments as: .zip

Change History (4)

by Johannes Brunen <JBrunen@…>, 14 years ago

Attachment: generator_iterator.hpp added

comment:1 by Daryle Walker, 14 years ago

Cc: dwalker07@… added

The author of the UUID library recently under review noticed this too; and made a copy of generator_iterator that adds explicit default-initialization for the generator pointer member. I realized that the cached value member also needs default initialization (which that author skipped). You mentioned comparisons with the generator pointer, but both members need it, as your code states, because both members will get compared when checking two end iterators. BTW, do the initializations need to be filled in with "0," or can nothing between the parentheses be used?

comment:2 by anonymous, 14 years ago

In case of the mersenne_twister generator the Generator::result_type is UIntType. Probably more correct would be initialization with Generator::result_type(), i.e.

generator_iterator() : m_g(0), m_value(Generator::result_type()) {}

Default construction of integral types are guaranteed to be 0.

comment:3 by Steven Watanabe, 12 years ago

Resolution: wontfix
Status: newclosed

Use the new function_input_iterator in Boost.Iterator instead. (See #2893). I'm going to deprecate generator_iterator as it's behavior can be somewhat odd.

Note: See TracTickets for help on using tickets.