From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 25F303857001; Thu, 1 Apr 2021 20:46:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25F303857001 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99805] [9/10/11 Regression] filesystem::path::parent_path got a wrong path Date: Thu, 01 Apr 2021 20:46:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: redi at gcc dot gnu.org X-Bugzilla-Target-Milestone: 9.4 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: 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: Thu, 01 Apr 2021 20:46:59 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99805 --- Comment #4 from Jonathan Wakely --- Wow, this is tricksy. The bug happens when parsing the string into a path. The path is split into components and the offset of each component from the beginning of the strin= g is stored, so that parent_path() can efficiently erase the part of the native() string that refers to the last component. The offset is calculated like thi= s: for (auto& c : buf) { auto pos =3D c.str.data() - _M_pathname.data(); ::new(output++) _Cmpt(c.str, c.type, pos); ++_M_cmpts._M_impl->_M_size; } The bug is that for the COW std::string _M_pathname.data() causes the strin= g to be reallocated, and so changes the address at which the string is stored. So the string_view c.str no longer refers to the same data as _M_pathname. We = get a bogus offset for the components, and when we try to remove the last compo= nent to get the parent_path(), we don't remove anything from the string.=