public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Do not allocate a zero-size vector<bool> [PR 100153]
Date: Fri, 1 Oct 2021 20:40:31 +0100	[thread overview]
Message-ID: <YVdkL3tQZXGpAfvT@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 687 bytes --]

The vector<bool>::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 <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	PR libstdc++/100153
	* include/bits/vector.tcc (vector<bool>::_M_shrink_to_fit()):
	When size() is zero just deallocate and reset.

Tested powerpc64le-linux. Committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1443 bytes --]

commit 681707ec28d56494fa61a80c62500724d55f8586
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Apr 20 16:16:13 2021

    libstdc++: Do not allocate a zero-size vector<bool> [PR 100153]
    
    The vector<bool>::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 <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100153
            * include/bits/vector.tcc (vector<bool>::_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(...)

                 reply	other threads:[~2021-10-01 19:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YVdkL3tQZXGpAfvT@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).