public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/100153] New: Undefined behavior in stl_bvector.h
@ 2021-04-20 14:10 hewillk at gmail dot com
  2021-04-20 15:05 ` [Bug libstdc++/100153] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: hewillk at gmail dot com @ 2021-04-20 14:10 UTC (permalink / raw)
  To: gcc-bugs

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().

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-05-09 16:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-20 14:10 [Bug libstdc++/100153] New: Undefined behavior in stl_bvector.h hewillk at gmail dot com
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

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).