From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 811873858D33 for ; Fri, 6 Jan 2023 14:12:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 811873858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673014325; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=II+gUA9M+dpDYAupi4OkgHjcmyTF2LRDtptC5fF7i+4=; b=YflywmSgUVxnXh4Ipf8EMdxAT40ky1cVWUJAkguzEhefS8sdJbCEVGRNNQDRi/UQrnsH4b e0yPuhlkSE9MzfD+l0s5mwq8Ih408e/U+irhpHuWlsgWpOK7Ny4Rn7Mmz6zCQxA5Vbcr5U R1bU5Jy4g57yf3f1ubp7SgnXw51LwtA= 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) From: Jonathan Wakely 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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; } -- 2.39.0