From patchwork Fri Jan 6 14:12:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 40144 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp845608wrt; Fri, 6 Jan 2023 06:12:51 -0800 (PST) X-Google-Smtp-Source: AMrXdXu1kdgImWJYplQWSPEB69o7ErJctXMyCIsgptCjCyqpzigphS/hqvkCr0k90LKfZVkwi+O/ X-Received: by 2002:a17:907:a782:b0:7c1:6430:e5d0 with SMTP id vx2-20020a170907a78200b007c16430e5d0mr49393158ejc.4.1673014371464; Fri, 06 Jan 2023 06:12:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673014371; cv=none; d=google.com; s=arc-20160816; b=xYzZ0RtWMQOzjlqN+AQSOTMrxrVoiXyLiqQl5h7sNbqFcX0VdVsimZe/O7HQoCqIgL RYRGbrwsnT38xBUF4Essq8Jk+KM77ESDTTOrbX98/UWw/qTLOdy7mtLVeao3S1D63CZa hni1VHi2yApn9FvbNkk2EmKrKbdpZgqF86YW5KUpBnSgF4bdBpziYCxzKYEYr3YNcfrJ pTHhthb/mVJHnJvaCmJJvN+gCM67w3xKHzgvEdq98zSjKclxa+wxpB0jbTEGZsfTElY6 WX/xjUeTtl3xL3Un4Igh8WjWo9oK8FO0l2s4M96ukhGu1t2x5a3gc60SaPWGX69DVleU u5aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=II+gUA9M+dpDYAupi4OkgHjcmyTF2LRDtptC5fF7i+4=; b=fOavodIvTPReYuQiy+Fjkz5XlcDA8c4A18yZ9i22iBJqwnvrY9QOszKyZGXPfymt4s 3MM9MIZ/CApaBBeBwgcr/en2SKrbaNMnYnHV3VRcQKVtnVO7diMw345COqljrwpXhyQ3 URezBHTJnGIa1N6ATCaLlVlsoCXCqBMRvY/bkBluToSRzoEQq2eI9EzAJ4jJ51Vc5rJ5 zaei/SMkrii0IljwPFY7GfuPi6pSc222DeEabzf2/4loPbPV9ea9z30m5fk+DeevuEwI Xd9YaFfzFt8ilVOVHGJO5pW3AuTPv0HB5JTeRQPIxg6LI/Sg8A/OZ31Q/SuXRzX0wAMT ne0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FoWqEoKO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dp15-20020a170906c14f00b007c16b3feca1si1366491ejc.903.2023.01.06.06.12.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Jan 2023 06:12:51 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FoWqEoKO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ED8F13857C5A for ; Fri, 6 Jan 2023 14:12:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED8F13857C5A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673014367; bh=II+gUA9M+dpDYAupi4OkgHjcmyTF2LRDtptC5fF7i+4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=FoWqEoKO6TLU1LsBrChwhsYvLOaJEeTN9bhTVermS0A4ATEvxqvFLMn9+Z/daN/Ma D9wq+nQMOVXAkOpZte6Z9raGSAC5ENR9+uGCtfNyuwafc2b/H+wiHais68ArQswv3s 8UdhI993ibmC1h5cEt2reaAgmADxZEKQSp7uvwrI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 152953858D28 for ; Fri, 6 Jan 2023 14:12:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 152953858D28 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-439-6YbhBfTnOo-d6tOx8kvwSg-1; Fri, 06 Jan 2023 09:12:02 -0500 X-MC-Unique: 6YbhBfTnOo-d6tOx8kvwSg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 061363C38FF7; Fri, 6 Jan 2023 14:12:01 +0000 (UTC) Received: from localhost (unknown [10.33.36.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id C353239D6D; Fri, 6 Jan 2023 14:12:00 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix misuse of alloca in std::bitset [PR108214] Date: Fri, 6 Jan 2023 14:12:00 +0000 Message-Id: <20230106141200.237958-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754282717483504794?= X-GMAIL-MSGID: =?utf-8?q?1754282717483504794?= I done a silly. Tested x86_64-linux, pushed to trunk. -- >8 -- The use of alloca in a constructor is wrong, because the memory is gone after the constructor returns, and will be overwritten by a subsequent function call. This didn't show up in testing because function inlining alters the stack usage. libstdc++-v3/ChangeLog: PR libstdc++/108214 * include/std/bitset (operator>>): Use alloca in the right scope, not in a constructor. * testsuite/20_util/bitset/io/input.cc: Check case from PR. --- libstdc++-v3/include/std/bitset | 24 +++++++++++-------- .../testsuite/20_util/bitset/io/input.cc | 21 ++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset index 1f3f68fefce..edda0776629 100644 --- a/libstdc++-v3/include/std/bitset +++ b/libstdc++-v3/include/std/bitset @@ -1598,20 +1598,24 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER struct _Buffer { - _Buffer() - : _M_base(_Nb > 256 ? new _CharT[_Nb] : (_CharT*)__builtin_alloca(_Nb)) - { } + static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; } + + explicit _Buffer(_CharT* __p) : _M_ptr(__p) { } ~_Buffer() { - if _GLIBCXX17_CONSTEXPR (_Nb > 256) - delete[] _M_base; + if _GLIBCXX17_CONSTEXPR (!_S_use_alloca()) + delete[] _M_ptr; } - _CharT* const _M_base; + _CharT* const _M_ptr; }; - _Buffer __buf; - _CharT* __ptr = __buf._M_base; + _CharT* __ptr; + if _GLIBCXX17_CONSTEXPR (_Buffer::_S_use_alloca()) + __ptr = (_CharT*)__builtin_alloca(_Nb); + else + __ptr = new _CharT[_Nb]; + const _Buffer __buf(__ptr); // _GLIBCXX_RESOLVE_LIB_DEFECTS // 303. Bitset input operator underspecified @@ -1662,8 +1666,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER if _GLIBCXX17_CONSTEXPR (_Nb) { - if (size_t __len = __ptr - __buf._M_base) - __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_base, __len, + if (size_t __len = __ptr - __buf._M_ptr) + __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len, 0, __len, __zero, __one); else diff --git a/libstdc++-v3/testsuite/20_util/bitset/io/input.cc b/libstdc++-v3/testsuite/20_util/bitset/io/input.cc index 0f22cefbb5b..4f7e6281ac5 100644 --- a/libstdc++-v3/testsuite/20_util/bitset/io/input.cc +++ b/libstdc++-v3/testsuite/20_util/bitset/io/input.cc @@ -42,8 +42,29 @@ void test01() VERIFY( ss.rdstate() == ios_base::goodbit ); // LWG 3199 } +void +test02() +{ + std::bitset<4> a(0b1100), b; + std::stringstream ss; + ss << a; + ss >> b; // PR libstdc++/108214 + VERIFY( b == a ); + + ss.str(""); + ss.clear(); + + std::bitset<4000> c, d; + for (int i = 0; i < 4000; i += 5) + c.flip(i); + ss << c; + ss >> d; + VERIFY( d == c ); +} + int main() { test01(); + test02(); return 0; }