* [PATCH] gdbsupport: fix path_join crash with -std=c++17 and -D_GLIBCXX_DEBUG
@ 2022-05-04 14:18 Simon Marchi
2022-05-19 12:08 ` Simon Marchi
0 siblings, 1 reply; 2+ messages in thread
From: Simon Marchi @ 2022-05-04 14:18 UTC (permalink / raw)
To: gdb-patches
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<char>; 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<const gdb::string_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
--
2.36.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] gdbsupport: fix path_join crash with -std=c++17 and -D_GLIBCXX_DEBUG
2022-05-04 14:18 [PATCH] gdbsupport: fix path_join crash with -std=c++17 and -D_GLIBCXX_DEBUG Simon Marchi
@ 2022-05-19 12:08 ` Simon Marchi
0 siblings, 0 replies; 2+ messages in thread
From: Simon Marchi @ 2022-05-19 12:08 UTC (permalink / raw)
To: gdb-patches
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<char>; 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<const gdb::string_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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-05-19 12:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-04 14:18 [PATCH] gdbsupport: fix path_join crash with -std=c++17 and -D_GLIBCXX_DEBUG Simon Marchi
2022-05-19 12:08 ` Simon Marchi
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).