From patchwork Thu Aug 18 16:35:15 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 110534 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 187B2B6FA0 for ; Fri, 19 Aug 2011 02:35:54 +1000 (EST) Received: (qmail 24240 invoked by alias); 18 Aug 2011 16:35:41 -0000 Received: (qmail 24044 invoked by uid 22791); 18 Aug 2011 16:35:38 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from rcsinet15.oracle.com (HELO rcsinet15.oracle.com) (148.87.113.117) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 18 Aug 2011 16:35:24 +0000 Received: from rtcsinet21.oracle.com (rtcsinet21.oracle.com [66.248.204.29]) by rcsinet15.oracle.com (Switch-3.4.4/Switch-3.4.4) with ESMTP id p7IGZLV3018927 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 18 Aug 2011 16:35:22 GMT Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by rtcsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id p7IGZJF0009406 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 18 Aug 2011 16:35:20 GMT Received: from abhmt102.oracle.com (abhmt102.oracle.com [141.146.116.54]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id p7IGZE5H009761; Thu, 18 Aug 2011 11:35:14 -0500 Received: from [192.168.1.4] (/79.52.211.11) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 18 Aug 2011 09:35:14 -0700 Message-ID: <4E4D3F43.4020306@oracle.com> Date: Thu, 18 Aug 2011 18:35:15 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20110812 Thunderbird/6.0 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: libstdc++ Subject: [v3] Fix libstdc++/50119 X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi, note that the same issue affects the legacy __gnu_cxx::copy_n by HP/SGI but certainly I'm not going to change it now after so many years of people relying on that "broken" behavior. Tested x86_64-linux, committed. Paolo. /////////////////////////////// 2011-08-18 Paolo Carlini 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. Index: include/bits/stl_algo.h =================================================================== --- include/bits/stl_algo.h (revision 177866) +++ include/bits/stl_algo.h (working copy) @@ -979,11 +979,17 @@ __copy_n(_InputIterator __first, _Size __n, _OutputIterator __result, input_iterator_tag) { - for (; __n > 0; --__n) + if (__n > 0) { - *__result = *__first; - ++__first; - ++__result; + while (true) + { + *__result = *__first; + ++__result; + if (--__n > 0) + ++__first; + else + break; + } } return __result; } Index: testsuite/25_algorithms/copy_n/50119.cc =================================================================== --- testsuite/25_algorithms/copy_n/50119.cc (revision 0) +++ testsuite/25_algorithms/copy_n/50119.cc (revision 0) @@ -0,0 +1,52 @@ +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2011 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without Pred the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include +#include +#include + +// libstdc++/50119 +void test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + vector v; + istringstream s("1 2 3 4 5"); + + copy_n(istream_iterator(s), 2, back_inserter(v)); + VERIFY( v.size() == 2 ); + VERIFY( v[0] == 1 ); + VERIFY( v[1] == 2 ); + + copy_n(istream_iterator(s), 2, back_inserter(v)); + VERIFY( v.size() == 4 ); + VERIFY( v[0] == 1 ); + VERIFY( v[1] == 2 ); + VERIFY( v[2] == 3 ); + VERIFY( v[3] == 4 ); +} + +int main() +{ + test01(); + return 0; +}