From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id 162203858C62; Wed, 21 Sep 2022 15:38:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 162203858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1663774690; bh=1SFbGZqOSqcXtN+6WZxSrSkd9wC6Nmzh+KKzQ8p0tH0=; h=From:To:Subject:Date:From; b=AjfHN83aItYPw3JSy5FAd+8iwSWgO6VLxOpcIlNIu77Gjtwcwx1deRuXgBY1alDl3 qChp36oBZQTaK0d8vlu7oyC3x/giU4yQOFZbZWg+zfN1Y989mhd7JgW5kInxABWNrd +/HMBO1t3+dWAmhLY2s5SFCGqkNKsEaLDcDwvcE8= 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: change path_join parameter to array_view X-Act-Checkin: binutils-gdb X-Git-Author: Simon Marchi X-Git-Refname: refs/heads/master X-Git-Oldrev: df86565b31bf12aab6fdceade49169bc6f378b13 X-Git-Newrev: 6f1d2f789bfaf457565787ff8a59108039bc637e Message-Id: <20220921153810.162203858C62@sourceware.org> Date: Wed, 21 Sep 2022 15:38:10 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D6f1d2f789bfa= f457565787ff8a59108039bc637e commit 6f1d2f789bfaf457565787ff8a59108039bc637e Author: Simon Marchi Date: Tue Jul 19 10:27:41 2022 -0400 gdbsupport: change path_join parameter to array_view =20 When a GDB built with -D_GLIBCXX_DEBUG=3D1 reads a binary with a single character name, we hit this assertion failure: =20 $ ./gdb -q --data-directory=3Ddata-directory -nx ./x /usr/include/c++/12.1.0/string_view:239: constexpr const std::basic= _string_view<_CharT, _Traits>::value_type& std::basic_string_view<_CharT, _= Traits>::operator[](size_type) const [with _CharT =3D char; _Traits =3D std= ::char_traits; const_reference =3D const char&; size_type =3D long un= signed int]: Assertion '__pos < this->_M_len' failed. =20 The backtrace: =20 #3 0x00007ffff6c0f002 in std::__glibcxx_assert_fail (file=3D, line=3D, function=3D, condition=3D= ) at /usr/src/debug/gcc/libstdc++-v3/src/c++11/debug.cc:60 #4 0x000055555da8a864 in std::basic_string_view >::operator[] (this=3D0x7fffffffcc30, __pos=3D1) at /usr/include= /c++/12.1.0/string_view:239 #5 0x00005555609dcb88 in path_join[abi:cxx11](gdb::array_view > const>) (paths=3D...) at = /home/simark/src/binutils-gdb/gdbsupport/pathstuff.cc:203 #6 0x000055555e0443f4 in path_join () at= /home/simark/src/binutils-gdb/gdb/../gdbsupport/pathstuff.h:84 #7 0x00005555609dc336 in gdb_realpath_keepfile[abi:cxx11](char con= st*) (filename=3D0x6060000a8d40 "/home/simark/build/binutils-gdb-one-target= /gdb/./x") at /home/simark/src/binutils-gdb/gdbsupport/pathstuff.cc:122 #8 0x000055555ebd2794 in exec_file_attach (filename=3D0x7fffffffe0= f9 "./x", from_tty=3D1) at /home/simark/src/binutils-gdb/gdb/exec.c:471 #9 0x000055555f2b3fb0 in catch_command_errors (command=3D0x55555eb= d1ab6 , arg=3D0x7fffffffe0f9 "./x", fro= m_tty=3D1, do_bp_actions=3Dfalse) at /home/simark/src/binutils-gdb/gdb/main= .c:513 #10 0x000055555f2b7e11 in captured_main_1 (context=3D0x7fffffffdb60= ) at /home/simark/src/binutils-gdb/gdb/main.c:1209 #11 0x000055555f2b9144 in captured_main (data=3D0x7fffffffdb60) at = /home/simark/src/binutils-gdb/gdb/main.c:1319 #12 0x000055555f2b9226 in gdb_main (args=3D0x7fffffffdb60) at /home= /simark/src/binutils-gdb/gdb/main.c:1344 #13 0x000055555d938c5e in main (argc=3D5, argv=3D0x7fffffffdcf8) at= /home/simark/src/binutils-gdb/gdb/gdb.c:32 =20 The problem is this line in path_join: =20 gdb_assert (strlen (path) =3D=3D 0 || !IS_ABSOLUTE_PATH (path)); =20 ... where `path` is "x". IS_ABSOLUTE_PATH eventually calls HAS_DRIVE_SPEC_1: =20 #define HAS_DRIVE_SPEC_1(dos_based, f) \ ((f)[0] && ((f)[1] =3D=3D ':') && (dos_based)) =20 This macro accesses indices 0 and 1 of the input string. However, `f` is a string_view of length 1, so it's incorrect to try to access index 1. We know that the string_view's underlying object is a null-terminat= ed string, so in practice there's no harm. But as far as the string_view is concerned, index 1 is considered out of bounds. =20 This patch makes the easy fix, that is to change the path_join parameter from a vector of to a vector of `const char *`. Another solution would be to introduce a non-standard gdb::cstring_view class, which would be a view over a null-terminated string. With that class, it would be correct to access index 1, it would yield the NUL character. If there is interest in having this class (it has been mentioned a few times in the past) I can do it and use it here. =20 This was found by running tests such as gdb.ada/arrayidx.exp, which produce 1-char long filenames, so adding a new test is not necessary. =20 Change-Id: Ia41a16c7243614636b18754fd98a41860756f7af Diff: --- gdb/dwarf2/line-header.c | 4 ++-- gdbsupport/pathstuff.cc | 8 ++++---- gdbsupport/pathstuff.h | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c index 73db3765c09..3a47557806e 100644 --- a/gdb/dwarf2/line-header.c +++ b/gdb/dwarf2/line-header.c @@ -69,13 +69,13 @@ line_header::file_file_name (const file_entry &fe) const =20 const char *dir =3D fe.include_dir (this); if (dir !=3D nullptr) - ret =3D path_join (dir, ret); + ret =3D path_join (dir, ret.c_str ()); =20 if (IS_ABSOLUTE_PATH (ret)) return ret; =20 if (m_comp_dir !=3D nullptr) - ret =3D path_join (m_comp_dir, ret); + ret =3D path_join (m_comp_dir, ret.c_str ()); =20 return ret; } diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc index af10c6ebd2e..390f10b1b5e 100644 --- a/gdbsupport/pathstuff.cc +++ b/gdbsupport/pathstuff.cc @@ -191,21 +191,21 @@ child_path (const char *parent, const char *child) /* See gdbsupport/pathstuff.h. */ =20 std::string -path_join (gdb::array_view paths) +path_join (gdb::array_view paths) { std::string ret; =20 for (int i =3D 0; i < paths.size (); ++i) { - const gdb::string_view path =3D paths[i]; + const char *path =3D paths[i]; =20 if (i > 0) - gdb_assert (path.empty () || !IS_ABSOLUTE_PATH (path)); + gdb_assert (strlen (path) =3D=3D 0 || !IS_ABSOLUTE_PATH (path)); =20 if (!ret.empty () && !IS_DIR_SEPARATOR (ret.back ())) ret +=3D '/'; =20 - ret.append (path.begin (), path.end ()); + ret.append (path); } =20 return ret; diff --git a/gdbsupport/pathstuff.h b/gdbsupport/pathstuff.h index d01db89e085..e0b5bc4f355 100644 --- a/gdbsupport/pathstuff.h +++ b/gdbsupport/pathstuff.h @@ -67,7 +67,7 @@ extern const char *child_path (const char *parent, const = char *child); The first element can be absolute or relative. All the others must be relative. */ =20 -extern std::string path_join (gdb::array_view path= s); +extern std::string path_join (gdb::array_view paths); =20 /* Same as the above, but accept paths as distinct parameters. */ =20 @@ -78,10 +78,10 @@ path_join (Args... paths) /* It doesn't make sense to join less than two paths. */ gdb_static_assert (sizeof... (Args) >=3D 2); =20 - std::array views - { gdb::string_view (paths)... }; + std::array path_array + { paths... }; =20 - return path_join (gdb::array_view (views)); + return path_join (gdb::array_view (path_array)); } =20 /* Return whether PATH contains a directory separator character. */