From patchwork Mon Jun 8 13:03:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 481861 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id B01DC140082 for ; Mon, 8 Jun 2015 23:04:06 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=qfbZvEfe; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=qJpvcPoff7qJ1y4LiB21Tee8pAavB1d7okNEVdUj1bwYIVmW9gGIX yHIZoTQO2FgU5NkDjv8tIwOfqZI+rLM5RphhxiZYKkHlMvrwapwF0aR78Yec8kFm UFG04JugqsIyxb+FbLKsQ/vdnx1Xn+HT05upW/HzgQMeq++p4rPySQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=y45d4JGj7NfBbTMah1aknkanWl0=; b=qfbZvEfe4YJTPogkfLUY vlf81dmO/YFQd3Ur0+ruxNvsWdD7hmxeqcrTVU14FCZSaEZjJj376NQEmlLHCTZY dag7Ad29GEazo2inWY16bGlBNgH8DiOk2ezqhFpsy2OhNyXygJKOXdXElUEEoKsA cy4DxTJ9U9vLGTP/iDN1EgM= Received: (qmail 50878 invoked by alias); 8 Jun 2015 13:03:58 -0000 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 Received: (qmail 50859 invoked by uid 89); 8 Jun 2015 13:03:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=AWL, BAYES_05, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 08 Jun 2015 13:03:55 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 0D6BA19CB95; Mon, 8 Jun 2015 13:03:54 +0000 (UTC) Received: from localhost (ovpn-116-119.ams2.redhat.com [10.36.116.119]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t58D3rBt009609; Mon, 8 Jun 2015 09:03:53 -0400 Date: Mon, 8 Jun 2015 14:03:52 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [patch] libstdc++/66441 Fix wstring_convert when generating BOM Message-ID: <20150608130352.GX12728@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) If the codecvt facet generates a BOM on every call to out() then we need to ensure there is enough capacity in the output string for the entire result, otherwise we loop and insert more than one BOM into the result. Tested powerpc64-linux and powerpc64-linux. Committing to trunk and gcc-5-branch. commit 5767dd073ce2905f35d817f67df8a3a4d3c995dc Author: Jonathan Wakely Date: Mon Jun 8 13:15:33 2015 +0100 PR libstdc++/66441 * testsuite/22_locale/conversions/string/66441.cc: New. * include/bits/locale_conv.h (__do_str_codecvt): Reserve enough space in the output string for BOM and complete result. diff --git a/libstdc++-v3/include/bits/locale_conv.h b/libstdc++-v3/include/bits/locale_conv.h index 8b0a77c..61b535c 100644 --- a/libstdc++-v3/include/bits/locale_conv.h +++ b/libstdc++-v3/include/bits/locale_conv.h @@ -60,12 +60,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { size_t __outchars = 0; auto __next = __first; - const auto __maxlen = __cvt.max_length(); + const auto __maxlen = __cvt.max_length() + 1; codecvt_base::result __result; do { - __outstr.resize(__outstr.size() + (__last - __next) + __maxlen); + __outstr.resize(__outstr.size() + (__last - __next) * __maxlen); auto __outnext = &__outstr.front() + __outchars; auto const __outlast = &__outstr.back() + 1; __result = (__cvt.*__fn)(__state, __next, __last, __next, diff --git a/libstdc++-v3/testsuite/22_locale/conversions/string/66441.cc b/libstdc++-v3/testsuite/22_locale/conversions/string/66441.cc new file mode 100644 index 0000000..b72edc8 --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/conversions/string/66441.cc @@ -0,0 +1,49 @@ +// Copyright (C) 2015 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 even 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 +// . + +// { dg-options "-std=gnu++11" } + +// libstdc++/66441 + +#include +#include +#include + +void +test01() +{ + // convert from UCS-4 to UTF16BE with BOM. + using cvt = std::codecvt_utf16; + std::wstring_convert conv; + auto to = conv.to_bytes(U"ab\u00e7"); + + VERIFY( to.length() == 8 ); + VERIFY( (unsigned char)to[0] == 0xfe ); + VERIFY( (unsigned char)to[1] == 0xff ); + VERIFY( (unsigned char)to[2] == 0x00 ); + VERIFY( (unsigned char)to[3] == 0x61 ); + VERIFY( (unsigned char)to[4] == 0x00 ); + VERIFY( (unsigned char)to[5] == 0x62 ); + VERIFY( (unsigned char)to[6] == 0x00 ); + VERIFY( (unsigned char)to[7] == 0xe7 ); +} + +int +main() +{ + test01(); +}