From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 897413858025; Tue, 20 Apr 2021 14:10:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 897413858025 From: "hewillk at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: hewillk at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Apr 2021 14:10:57 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100153 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 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().=