public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hewillk at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/100153] New: Undefined behavior in stl_bvector.h
Date: Tue, 20 Apr 2021 14:10:57 +0000	[thread overview]
Message-ID: <bug-100153-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100153

            Bug ID: 100153
           Summary: Undefined behavior in stl_bvector.h
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

stl_bvector.h#L505:

  _Bit_type*
  _M_end_addr() const _GLIBCXX_NOEXCEPT
  {
    if (this->_M_end_of_storage)
      return std::__addressof(this->_M_end_of_storage[-1]) + 1;
    return 0;
  }

This one will cause UB if _M_end_of_storage is not nullptr and
_M_end_of_storage is equal to _M_start._M_p.

Consider (https://godbolt.org/z/vvrG6KcWP):

  std::vector<bool> bv{true, false, true};
  bv.clear();
  bv.shrink_to_fit();
  bv.capacity();

When shrink_to_fit() ends, _M_end_of_storage will be equal to _M_start._M_p and
not nullptr. In capacity():

  size_type
  capacity() const _GLIBCXX_NOEXCEPT
  { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
    - begin()); }

we will call _M_end_addr() and perform illegal dereference which cause UB, same
with push_back(), insert() and flip().

             reply	other threads:[~2021-04-20 14:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-20 14:10 hewillk at gmail dot com [this message]
2021-04-20 15:05 ` [Bug libstdc++/100153] " redi at gcc dot gnu.org
2021-04-20 15:32 ` redi at gcc dot gnu.org
2021-05-12  9:53 ` redi at gcc dot gnu.org
2021-10-01 19:38 ` cvs-commit at gcc dot gnu.org
2021-10-01 19:51 ` redi at gcc dot gnu.org
2021-11-24 11:50 ` cvs-commit at gcc dot gnu.org
2022-04-26 13:13 ` cvs-commit at gcc dot gnu.org
2022-05-09 16:40 ` cvs-commit at gcc dot gnu.org
2022-05-09 16:47 ` redi at gcc dot gnu.org

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=bug-100153-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).