Bug in include/bits/stl_iterator.h?

Nathan Myers ncm-nospam@cantrip.org
Fri May 30 01:48:00 GMT 2003


In include/bits/stl_iterator.h, we have

    /**
    *  The default constructor gives an undefined state to this
    *  %iterator.
    */
    reverse_iterator() { }

And in include/bits/basic_string.tcc (line 144), _S_construct, we have

    // NB: Not required, but considered best practice.
    if (__builtin_expect(__beg == _InIter(), 0))
      __throw_logic_error("basic_string::_S_construct NULL not valid");

It's entirely possible for a valid input value __beg to accidently 
match the (uninitialized) value that shows up in the temporary value
_InIter() constructed on the stack, such as  when constructing a 
string from a reverse_iterator on valid pointer values.

The effect of this problem is that valid user programs may throw, 
nondeterministically.

According to 24.1 [lib.iterator.requirements] par 5 (p510), it's not 
allowed to pass a default-constructed iterator to op==.  Therefore, 
we should eliminate this check from basic_string.tcc and anywhere 
else it (or its like) appears.  Not only does it rely on behavior 
defined neither by the standard nor by us, it also breaks correct 
programs in an unpredictable and not necessarily repeatable way.

Comments?

Nathan Myers
ncm-nospam@cantrip.org



More information about the Libstdc++ mailing list