From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 25DDD3858C33; Mon, 30 Jan 2023 17:47:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25DDD3858C33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675100826; bh=lBTm+zhLg0ljiQydjVFm/vrZaeIzXSXmbU9NeruWIzs=; h=From:To:Subject:Date:From; b=tF7YbnNcRVGKWZAPsUwvMjnoh3aMs5zOueMEW6jrENu5Kz6vSsgCnt6zD1MlbGsUh MvMRi6wAz1Pa+9MnAE8ui51ne8M0LaYfB4xItfLgNMXP6bySAhV29e0gxevv6LrKzl 2daUepMb3gEfuKWw2HFQPwkRoukarWIUNos+H7oU= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Fix comparator bug in cooked index X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 9d00e8d76a7d7fd4a2029e9a47607e73d5aa9536 X-Git-Newrev: c121e82c39659d1140b1a6a3cfd72c765741b9f5 Message-Id: <20230130174706.25DDD3858C33@sourceware.org> Date: Mon, 30 Jan 2023 17:47:06 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dc121e82c3965= 9d1140b1a6a3cfd72c765741b9f5 commit c121e82c39659d1140b1a6a3cfd72c765741b9f5 Author: Tom Tromey Date: Fri Jan 27 11:19:16 2023 -0700 Fix comparator bug in cooked index =20 Simon pointed out that the cooked index template-matching patch introduced a failure in libstdc++ debug mode. In particular, the new code violates the assumption of std::lower_bound and std::upper_bound that the range is sorted with respect to the comparison. =20 When I first debugged this, I thought the problem was unfixable as-is and that a second layer of filtering would have to be done. However, on irc, Simon pointed out that it could perhaps be solved if the comparison function were assured that one operand always came from the index, with the other always being the search string. =20 This patch implements this idea. =20 First, a new mode is introduced: a sorting mode for cooked_index_entry::compare. In this mode, strings are compared case-insensitively, but we're careful to always sort '<' before any other printable character. This way, two names like "func" and "func" will be sorted next to each other -- i.e., "func1" will not be seen between them. This is important when searching. =20 Second, the compare function is changed to work in a strcmp-like way. This makes it easier to test and (IMO) understand. =20 Third, the compare function is modified so that in non-sorting modes, the index entry is always the first argument. This allows consistency in compares. =20 I regression tested this in libstdc++ debug mode on x86-64 Fedora 36. It fixes the crash that Simon saw. =20 This is v2. I believe it addresses the review comments, except for the 'enum class' change, as I mentioned in email on the list. =20 Approved-By: Simon Marchi Diff: --- gdb/dwarf2/cooked-index.c | 167 ++++++++++++++++++++++++------------------= ---- gdb/dwarf2/cooked-index.h | 50 ++++++++++++-- 2 files changed, 132 insertions(+), 85 deletions(-) diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index 646bc76575c..09d76523419 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -30,56 +30,43 @@ =20 /* See cooked-index.h. */ =20 -bool +int cooked_index_entry::compare (const char *stra, const char *strb, - bool completing) + comparison_mode mode) { - /* If we've ever matched "<" in both strings, then we disable the - special template parameter handling. */ - bool seen_lt =3D false; + auto munge =3D [] (char c) -> unsigned char + { + /* We want to sort '<' before any other printable character. + So, rewrite '<' to something just before ' '. */ + if (c =3D=3D '<') + return '\x1f'; + return TOLOWER ((unsigned char) c); + }; =20 while (*stra !=3D '\0' && *strb !=3D '\0' - && (TOLOWER ((unsigned char) *stra) - =3D=3D TOLOWER ((unsigned char ) *strb))) + && (munge (*stra) =3D=3D munge (*strb))) { - if (*stra =3D=3D '<') - seen_lt =3D true; ++stra; ++strb; } =20 - unsigned c1 =3D TOLOWER ((unsigned char) *stra); - unsigned c2 =3D TOLOWER ((unsigned char) *strb); + unsigned char c1 =3D munge (*stra); + unsigned char c2 =3D munge (*strb); =20 - if (completing) - { - /* When completing, if one string ends earlier than the other, - consider them as equal. Also, completion mode ignores the - special '<' handling. */ - if (c1 =3D=3D '\0' || c2 =3D=3D '\0') - return false; - /* Fall through to the generic case. */ - } - else if (seen_lt) - { - /* Fall through to the generic case. */ - } - else if (c1 =3D=3D '\0' || c1 =3D=3D '<') - { - /* Maybe they both end at the same spot. */ - if (c2 =3D=3D '\0' || c2 =3D=3D '<') - return false; - /* First string ended earlier. */ - return true; - } - else if (c2 =3D=3D '\0' || c2 =3D=3D '<') + if (c1 =3D=3D c2) + return 0; + + /* When completing, if STRB ends earlier than STRA, consider them as + equal. When comparing, if STRB ends earlier and STRA ends with + '<', consider them as equal. */ + if (mode =3D=3D COMPLETE || (mode =3D=3D MATCH && c1 =3D=3D munge ('<'))) { - /* Second string ended earlier. */ - return false; + if (c2 =3D=3D '\0') + return 0; } =20 - return c1 < c2; + return c1 < c2 ? -1 : 1; } =20 #if GDB_SELF_TEST @@ -89,45 +76,65 @@ namespace { void test_compare () { - SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", false)); - SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", false)); - SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", true)); - SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", true)); - - SELF_CHECK (cooked_index_entry::compare ("abcd", "ABCDE", false)); - SELF_CHECK (!cooked_index_entry::compare ("ABCDE", "abcd", false)); - SELF_CHECK (!cooked_index_entry::compare ("abcd", "ABCDE", true)); - SELF_CHECK (!cooked_index_entry::compare ("ABCDE", "abcd", true)); - - SELF_CHECK (!cooked_index_entry::compare ("name", "name<>", false)); - SELF_CHECK (!cooked_index_entry::compare ("name<>", "name", false)); - SELF_CHECK (!cooked_index_entry::compare ("name", "name<>", true)); - SELF_CHECK (!cooked_index_entry::compare ("name<>", "name", true)); - - SELF_CHECK (!cooked_index_entry::compare ("name", "name", fals= e)); - SELF_CHECK (!cooked_index_entry::compare ("name", "name", fals= e)); - SELF_CHECK (!cooked_index_entry::compare ("name", "name", true= )); - SELF_CHECK (!cooked_index_entry::compare ("name", "name", true)= ); - - SELF_CHECK (!cooked_index_entry::compare ("name>", - "name>", false)); - - SELF_CHECK (!cooked_index_entry::compare ("name", "name>", fal= se)); - SELF_CHECK (!cooked_index_entry::compare ("name>", "name", fal= se)); - SELF_CHECK (cooked_index_entry::compare ("name>", - false)); - SELF_CHECK (!cooked_index_entry::compare ("name>", - true)); - SELF_CHECK (!cooked_index_entry::compare ("name>", "name>", "name 0); + SELF_CHECK (cooked_index_entry::compare ("abcd", "ABCDE", + mode_complete) < 0); + SELF_CHECK (cooked_index_entry::compare ("ABCDE", "abcd", + mode_complete) =3D=3D 0); + + SELF_CHECK (cooked_index_entry::compare ("name", "name<>", + mode_compare) < 0); + SELF_CHECK (cooked_index_entry::compare ("name<>", "name", + mode_compare) =3D=3D 0); + SELF_CHECK (cooked_index_entry::compare ("name", "name<>", + mode_complete) < 0); + SELF_CHECK (cooked_index_entry::compare ("name<>", "name", + mode_complete) =3D=3D 0); + + SELF_CHECK (cooked_index_entry::compare ("name", "name", + mode_compare) =3D=3D 0); + SELF_CHECK (cooked_index_entry::compare ("name", "name", + mode_compare) > 0); + SELF_CHECK (cooked_index_entry::compare ("name", "name", + mode_complete) =3D=3D 0); + SELF_CHECK (cooked_index_entry::compare ("name", "name", + mode_complete) > 0); + + SELF_CHECK (cooked_index_entry::compare ("name>", + "name>", + mode_compare) =3D=3D 0); + + SELF_CHECK (cooked_index_entry::compare ("name", "name>", + mode_compare) < 0); + SELF_CHECK (cooked_index_entry::compare ("name>", "name", + mode_compare) =3D=3D 0); + SELF_CHECK (cooked_index_entry::compare ("name>", "name 0); + SELF_CHECK (cooked_index_entry::compare ("name>", "name 0); + SELF_CHECK (cooked_index_entry::compare ("abcd", "", mode_complete) =3D= =3D 0); + + SELF_CHECK (cooked_index_entry::compare ("func", "func", + mode_sort) < 0); + SELF_CHECK (cooked_index_entry::compare ("func", "func1", + mode_sort) < 0); } =20 } /* anonymous namespace */ @@ -359,20 +366,22 @@ cooked_index::find (const std::string &name, bool com= pleting) const { wait (); =20 + cooked_index_entry::comparison_mode mode =3D (completing + ? cooked_index_entry::COMPLETE + : cooked_index_entry::MATCH); + auto lower =3D std::lower_bound (m_entries.cbegin (), m_entries.cend (),= name, [=3D] (const cooked_index_entry *entry, const std::string &n) { - return cooked_index_entry::compare (entry->canonical, n.c_str (), - completing); + return cooked_index_entry::compare (entry->canonical, n.c_str (), mode= ) < 0; }); =20 auto upper =3D std::upper_bound (m_entries.cbegin (), m_entries.cend (),= name, [=3D] (const std::string &n, const cooked_index_entry *entry) { - return cooked_index_entry::compare (n.c_str (), entry->canonical, - completing); + return cooked_index_entry::compare (entry->canonical, n.c_str (), mode= ) > 0; }); =20 return range (lower, upper); diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h index 7299a4f77b4..891659f56e0 100644 --- a/gdb/dwarf2/cooked-index.h +++ b/gdb/dwarf2/cooked-index.h @@ -143,16 +143,54 @@ struct cooked_index_entry : public allocate_on_obstack STORAGE. */ const char *full_name (struct obstack *storage) const; =20 - /* Compare two strings, case-insensitively. Return true if STRA is - less than STRB. If one string has template parameters, but the - other does not, then they are considered to be equal; so for - example "t" =3D=3D "t", "t" < "t", but "t" =3D=3D "t".= */ - static bool compare (const char *stra, const char *strb, bool completing= ); + /* Comparison modes for the 'compare' function. See the function + for a description. */ + enum comparison_mode + { + MATCH, + SORT, + COMPLETE, + }; + + /* Compare two strings, case-insensitively. Return -1 if STRA is + less than STRB, 0 if they are equal, and 1 if STRA is greater. + + When comparing, '<' is considered to be less than all other + printable characters. This ensures that "t" sorts before + "t1", which is necessary when looking up "t". This '<' handling + is to ensure that certain C++ lookups work correctly. It is + inexact, and applied regardless of the search language, but this + is ok because callers of this code do more precise filtering + according to their needs. This is also why using a + case-insensitive comparison works even for languages that are + case sensitive. + + MODE controls how the comparison proceeds. + + MODE=3D=3DSORT is used when sorting and the only special '<' handling + that it does is to ensure that '<' sorts before all other + printable characters. This ensures that the resulting ordering + will be binary-searchable. + + MODE=3D=3DMATCH is used when searching for a symbol. In this case, + STRB must always be the search name, and STRA must be the name in + the index that is under consideration. In compare mode, early + termination of STRB may match STRA -- for example, "t" and + "t" will be considered to be equal. (However, if A=3D=3D"t" and + B=3D=3D"t", then this will not consider them as equal.) + + MODE=3D=3DCOMPLETE is used when searching for a symbol for + completion. In this case, STRB must always be the search name, + and STRA must be the name in the index that is under + consideration. In completion mode, early termination of STRB + always results in a match. */ + static int compare (const char *stra, const char *strb, + comparison_mode mode); =20 /* Compare two entries by canonical name. */ bool operator< (const cooked_index_entry &other) const { - return compare (canonical, other.canonical, false); + return compare (canonical, other.canonical, SORT) < 0; } =20 /* The name as it appears in DWARF. This always points into one of