Bug 50119 - [C++0x] copy_n advances InputIterator one more time than necessary
Summary: [C++0x] copy_n advances InputIterator one more time than necessary
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.7.0
Assignee: Paolo Carlini
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-18 14:55 UTC by Sergey Zubkov
Modified: 2017-08-24 11:44 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-08-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergey Zubkov 2011-08-18 14:55:39 UTC
The naive implementation of std::copy_n() incorrectly increments InputIterator one more time than needed, which fails with single-pass input iterators such as istream_iterator

Test program:

#include <algorithm>
#include <vector>
#include <sstream>
#include <iterator>
#include <iostream>
int main()
{
    std::istringstream s("1 2 3 4 5");
    std::vector<int> v;
    copy_n(std::istream_iterator<int>(s), 2, back_inserter(v));
    copy_n(std::istream_iterator<int>(s), 2, back_inserter(v));

    copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

Output with GCC 4.5.3 and 4.7.0-alpha20110716
1 2 4 5
expected output:
1 2 3 4

Similar bug was fixed in LLVM's libc++ in february 2011, the fix is straightforward: http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20110221/039404.html
Comment 1 Paolo Carlini 2011-08-18 15:03:26 UTC
Ok.
Comment 2 paolo@gcc.gnu.org 2011-08-18 16:32:31 UTC
Author: paolo
Date: Thu Aug 18 16:32:23 2011
New Revision: 177871

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=177871
Log:
2011-08-18  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/50119
	* include/bits/stl_algo.h (__copy_n(_InputIterator, _Size,
	_OutputIterator, input_iterator_tag)): Fix.
	* testsuite/25_algorithms/copy_n/50119.cc: New.

Added:
    trunk/libstdc++-v3/testsuite/25_algorithms/copy_n/50119.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/stl_algo.h
Comment 3 Paolo Carlini 2011-08-18 16:36:16 UTC
Done, thanks!