From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id 3E7CB38654B0; Sat, 14 Jan 2023 00:16:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E7CB38654B0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673655411; bh=WUDN64Tcu2M4ZXSet1UUIJS4CeorYUK/3GzujEGKKqs=; h=From:To:Subject:Date:From; b=DRl6+YEw60sZA6FvbFvaUvdYgoDCPyf9qczUMhhQqFC8gX4qezktBniyuoSC37+bL jHgEPc9vcRV0Y0Eo9UzuHBIdStzNtOYMeV3Y/F7IDfYR9I26hEBCqJRPp2St+wt79F su0C0yXRO3MC2Fp5SMW6XKHLTDMEtrC9KBAsbMHk= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5162] hash table: enforce testing is_empty before is_deleted X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/heads/master X-Git-Oldrev: 31aaa6ef5a952d4f64fb04010459f28e0e793702 X-Git-Newrev: 88679960c2665d87c8354ce35a48aaadbe3f0793 Message-Id: <20230114001651.3E7CB38654B0@sourceware.org> Date: Sat, 14 Jan 2023 00:16:51 +0000 (GMT) List-Id: https://gcc.gnu.org/g:88679960c2665d87c8354ce35a48aaadbe3f0793 commit r13-5162-g88679960c2665d87c8354ce35a48aaadbe3f0793 Author: Alexandre Oliva Date: Fri Jan 13 21:15:44 2023 -0300 hash table: enforce testing is_empty before is_deleted Existing hash_table traits that use the same representation for empty and deleted slots reject marking slots as deleted, and to not pass is_deleted for slots that pass is_empty. Nevertheless, nearly everywhere, we only test for is_deleted after checking that !is_empty first. The one exception was the copy constructor, that would fail if traits recognized is_empty slots as is_deleted, but then refused to mark_deleted. This asymmetry is neither necessary nor desirable, and there is a theoretical risk that traits might not only fail to refuse to mark_deleted, but also return is_deleted for is_empty slots. This patch introduces checks that detect these potentially problematic situations, and reorders the tests in the copy constructor so as to use the conventional testing order and thus avoid them. for gcc/ChangeLog * hash-table.h (is_deleted): Precheck !is_empty. (mark_deleted): Postcheck !is_empty. (copy constructor): Test is_empty before is_deleted. Diff: --- gcc/hash-table.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gcc/hash-table.h b/gcc/hash-table.h index 1d3166504c3..e37625dc315 100644 --- a/gcc/hash-table.h +++ b/gcc/hash-table.h @@ -534,6 +534,11 @@ private: void expand (); static bool is_deleted (value_type &v) { + /* Traits are supposed to avoid recognizing elements as both empty + and deleted, but to fail safe in case custom traits fail to do + that, make sure we never test for is_deleted without having + first ruled out is_empty. */ + gcc_checking_assert (!Descriptor::is_empty (v)); return Descriptor::is_deleted (v); } @@ -545,6 +550,11 @@ private: static void mark_deleted (value_type &v) { Descriptor::mark_deleted (v); + /* Traits are supposed to refuse to set elements as deleted if + those would be indistinguishable from empty, but to fail safe + in case custom traits fail to do that, check that the + just-deleted element does not look empty. */ + gcc_checking_assert (!Descriptor::is_empty (v)); } static void mark_empty (value_type &v) @@ -700,9 +710,11 @@ hash_table::hash_table (const hash_table &h, for (size_t i = 0; i < size; ++i) { value_type &entry = h.m_entries[i]; - if (is_deleted (entry)) + if (is_empty (entry)) + continue; + else if (is_deleted (entry)) mark_deleted (nentries[i]); - else if (!is_empty (entry)) + else new ((void*) (nentries + i)) value_type (entry); } m_entries = nentries;