From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id 332A5384B83F; Thu, 19 May 2022 12:08:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 332A5384B83F Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Simon Marchi To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdbsupport: fix path_join crash with -std=c++17 and -D_GLIBCXX_DEBUG X-Act-Checkin: binutils-gdb X-Git-Author: Simon Marchi X-Git-Refname: refs/heads/master X-Git-Oldrev: 15b7af6c874610d802b64e1778202b7653d5fa08 X-Git-Newrev: 02646f1960a53e50c05218725cbf63098bee5bbe Message-Id: <20220519120845.332A5384B83F@sourceware.org> Date: Thu, 19 May 2022 12:08:45 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 May 2022 12:08:45 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D02646f1960a5= 3e50c05218725cbf63098bee5bbe commit 02646f1960a53e50c05218725cbf63098bee5bbe Author: Simon Marchi Date: Wed May 4 10:10:25 2022 -0400 gdbsupport: fix path_join crash with -std=3Dc++17 and -D_GLIBCXX_DEBUG =20 When building GDB with -std=3Dc++17 and -D_GLIBCXX_DEBUG=3D1, I get: =20 $ ./gdb -nx --data-directory=3Ddata-directory -q -ex "maint selftest = path_join" /usr/include/c++/11.2.0/string_view:233: constexpr const value_type& = std::basic_string_view<_CharT, _Traits>::operator[](std::basic_string_view<= _CharT, _Traits>::size_type) const [with _CharT =3D char; _Traits =3D std::= char_traits; std::basic_string_view<_CharT, _Traits>::const_reference= =3D const char&; std::basic_string_view<_CharT, _Traits>::size_type =3D lo= ng unsigned int]: Assertion '__pos < this->_M_len' failed. =20 The problem is that we're passing an empty string_view to IS_ABSOLUTE_PATH. IS_ABSOLUTE_PATH accesses [0] on that string_view, which is out-of-bounds. =20 The reason this is not seen with -std less than c++17 is that our local copy of string_view (used with C++ < 17) does not have the assert in operator[], as that wouldn't work in a constexpr method: =20 https://gitlab.com/gnutools/binutils-gdb/-/blob/5890af36e5112bcbb8d75= 55e63570f68466e6944/gdbsupport/gdb_string_view.h#L180 =20 IS_ABSOLUTE_PATH is normally used with null-terminated string. It's fine to pass an empty null-terminated string to IS_ABSOLUTE_PATH, because index 0 in such a string is valid. But not with an empty string_view. =20 Fix that by avoiding the "call" to IS_ABSOLUTE_PATH if the string_view is empty. =20 Change-Id: Idf4df961b63f513b3389235e93814c02b89ea32e Diff: --- gdbsupport/pathstuff.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc index 5b5a8eea904..af10c6ebd2e 100644 --- a/gdbsupport/pathstuff.cc +++ b/gdbsupport/pathstuff.cc @@ -200,7 +200,7 @@ path_join (gdb::array_view path= s) const gdb::string_view path =3D paths[i]; =20 if (i > 0) - gdb_assert (!IS_ABSOLUTE_PATH (path)); + gdb_assert (path.empty () || !IS_ABSOLUTE_PATH (path)); =20 if (!ret.empty () && !IS_DIR_SEPARATOR (ret.back ())) ret +=3D '/';