From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 9BDEB384F021 for ; Thu, 19 May 2022 12:09:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9BDEB384F021 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 24JC8tSf005785 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 19 May 2022 08:08:59 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 24JC8tSf005785 Received: from [10.0.0.11] (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id EFADF1E143 for ; Thu, 19 May 2022 08:08:54 -0400 (EDT) Message-ID: <95cfc364-3ec6-6ebb-bc28-26963a8198fd@polymtl.ca> Date: Thu, 19 May 2022 08:08:54 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH] gdbsupport: fix path_join crash with -std=c++17 and -D_GLIBCXX_DEBUG Content-Language: en-US To: gdb-patches@sourceware.org References: <20220504141840.1269433-1-simon.marchi@polymtl.ca> From: Simon Marchi In-Reply-To: <20220504141840.1269433-1-simon.marchi@polymtl.ca> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Thu, 19 May 2022 12:08:55 +0000 X-Spam-Status: No, score=-3038.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 May 2022 12:09:02 -0000 On 2022-05-04 10:18, Simon Marchi wrote: > When building GDB with -std=c++17 and -D_GLIBCXX_DEBUG=1, I get: > > $ ./gdb -nx --data-directory=data-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 = char; _Traits = std::char_traits; std::basic_string_view<_CharT, _Traits>::const_reference = const char&; std::basic_string_view<_CharT, _Traits>::size_type = long unsigned int]: Assertion '__pos < this->_M_len' failed. > > 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. > > 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: > > https://gitlab.com/gnutools/binutils-gdb/-/blob/5890af36e5112bcbb8d7555e63570f68466e6944/gdbsupport/gdb_string_view.h#L180 > > 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. > > Fix that by avoiding the "call" to IS_ABSOLUTE_PATH if the string_view > is empty. > > Change-Id: Idf4df961b63f513b3389235e93814c02b89ea32e > --- > gdbsupport/pathstuff.cc | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc > index 5b5a8eea9047..af10c6ebd2e8 100644 > --- a/gdbsupport/pathstuff.cc > +++ b/gdbsupport/pathstuff.cc > @@ -200,7 +200,7 @@ path_join (gdb::array_view paths) > const gdb::string_view path = paths[i]; > > if (i > 0) > - gdb_assert (!IS_ABSOLUTE_PATH (path)); > + gdb_assert (path.empty () || !IS_ABSOLUTE_PATH (path)); > > if (!ret.empty () && !IS_DIR_SEPARATOR (ret.back ())) > ret += '/'; > > base-commit: 5890af36e5112bcbb8d7555e63570f68466e6944 I pushed this. Simon