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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id EA95F385782D for ; Fri, 1 Oct 2021 19:40:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EA95F385782D Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-281-VJbW--ILMjGwPf6xf5eo1g-1; Fri, 01 Oct 2021 15:40:34 -0400 X-MC-Unique: VJbW--ILMjGwPf6xf5eo1g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0A4D11084684; Fri, 1 Oct 2021 19:40:33 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id B1C426060F; Fri, 1 Oct 2021 19:40:32 +0000 (UTC) Date: Fri, 1 Oct 2021 20:40:31 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Do not allocate a zero-size vector [PR 100153] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="wTzXAMikieOQ73w3" Content-Disposition: inline X-Spam-Status: No, score=-13.8 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Oct 2021 19:40:37 -0000 --wTzXAMikieOQ73w3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The vector::shrink_to_fit() implementation will allocate new storage even if the vector is empty. That then leads to the end-of-storage pointer being non-null and equal to the _M_start._M_p pointer, which means that _M_end_addr() has undefined behaviour. The fix is to stop doing a useless zero-sized allocation in shrink_to_fit(), so that _M_start._M_p and _M_end_of_storage are both null after an empty vector shrinks. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/100153 * include/bits/vector.tcc (vector::_M_shrink_to_fit()): When size() is zero just deallocate and reset. Tested powerpc64le-linux. Committed to trunk. --wTzXAMikieOQ73w3 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 681707ec28d56494fa61a80c62500724d55f8586 Author: Jonathan Wakely Date: Tue Apr 20 16:16:13 2021 libstdc++: Do not allocate a zero-size vector [PR 100153] The vector::shrink_to_fit() implementation will allocate new storage even if the vector is empty. That then leads to the end-of-storage pointer being non-null and equal to the _M_start._M_p pointer, which means that _M_end_addr() has undefined behaviour. The fix is to stop doing a useless zero-sized allocation in shrink_to_fit(), so that _M_start._M_p and _M_end_of_storage are both null after an empty vector shrinks. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/100153 * include/bits/vector.tcc (vector::_M_shrink_to_fit()): When size() is zero just deallocate and reset. diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index caee5cbfc2f..16366e03c86 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -944,7 +944,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER return false; __try { - _M_reallocate(size()); + if (size_type __n = size()) + _M_reallocate(__n); + else + { + this->_M_deallocate(); + this->_M_impl._M_reset(); + } return true; } __catch(...) --wTzXAMikieOQ73w3--