Bug 53515 - InputIterator version std::advance needs negative check
Summary: InputIterator version std::advance needs negative check
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.7.0
: P3 enhancement
Target Milestone: 4.8.0
Assignee: Paolo Carlini
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-29 08:09 UTC by Akira Takahashi
Modified: 2012-09-26 23:57 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-05-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Akira Takahashi 2012-05-29 08:09:01 UTC
C++11 specification 24.4.4

template <class InputIterator, class Distance>
void advance(InputIterator& i, Distance n);

Requires: n shall be negative only for bidirectional and random access iterators.

==
libstdc++ std::advance is nothing positive check.
I think InputIterator version std::advance needs positive check.


before code:

template<typename _InputIterator, typename _Distance>
inline void
__advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
{
  // concept requirements
  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  while (__n--)
    ++__i;
}


after code (add assert):

template<typename _InputIterator, typename _Distance>
inline void
__advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
{
  // concept requirements
  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  _GLIBCXX_DEBUG_ASSERT(__n >= 0);
  while (__n--)
    ++__i;
}
Comment 1 Jonathan Wakely 2012-05-29 09:27:34 UTC
Good idea, thanks
Comment 2 Paolo Carlini 2012-09-26 23:21:34 UTC
I'm going to test and apply this.
Comment 3 paolo@gcc.gnu.org 2012-09-26 23:56:43 UTC
Author: paolo
Date: Wed Sep 26 23:56:39 2012
New Revision: 191784

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=191784
Log:
2012-09-26  Akira Takahashi  <faithandbrave@gmail.com>

	PR libstdc++/53515
	* include/bits/stl_iterator_base_funcs.h (__advance(_InputIterator&,
	_Distance, input_iterator_tag)): Add _GLIBCXX_DEBUG_ASSERT(__n >= 0).

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/stl_iterator_base_funcs.h
Comment 4 Paolo Carlini 2012-09-26 23:57:29 UTC
Done.