From fe6fb0d1590237c67b75342dc41b7ffdbdcccf25 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Sat, 4 May 2019 15:35:25 +0100 Subject: [PATCH] Fix std::hash The hash value should be based on the identity (i.e. address) of the error_category member, not its object representation (i.e. underlying bytes). * include/std/system_error (error_code): Remove friend declaration for hash. (hash::operator()): Use public member functions to access value and category. (hash::operator()): Use address of category, not its object representation. * src/c++11/compatibility-c++0x.cc (hash::operator()): Use public member functions to access value and category. * testsuite/19_diagnostics/error_condition/hash.cc: New test. From-SVN: r270872 --- libstdc++-v3/ChangeLog | 12 +++++ libstdc++-v3/include/std/system_error | 10 ++-- libstdc++-v3/src/c++11/compatibility-c++0x.cc | 4 +- .../19_diagnostics/error_condition/hash.cc | 51 +++++++++++++++++++ 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c434fbcae5b..9965009eb3e 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2019-05-04 Jonathan Wakely + + * include/std/system_error (error_code): Remove friend declaration + for hash. + (hash::operator()): Use public member functions to access + value and category. + (hash::operator()): Use address of category, not + its object representation. + * src/c++11/compatibility-c++0x.cc (hash::operator()): + Use public member functions to access value and category. + * testsuite/19_diagnostics/error_condition/hash.cc: New test. + 2019-05-04 François Dumont PR libstdc++/90277 diff --git a/libstdc++-v3/include/std/system_error b/libstdc++-v3/include/std/system_error index b7891cbaa86..a60c96accb2 100644 --- a/libstdc++-v3/include/std/system_error +++ b/libstdc++-v3/include/std/system_error @@ -193,8 +193,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // DR 804. private: - friend class hash; - int _M_value; const error_category* _M_cat; }; @@ -394,13 +392,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION size_t operator()(const error_code& __e) const noexcept { - const size_t __tmp = std::_Hash_impl::hash(__e._M_value); - return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp); + const size_t __tmp = std::_Hash_impl::hash(__e.value()); + return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); } }; #endif // _GLIBCXX_COMPATIBILITY_CXX0X -#if __cplusplus > 201402L +#if __cplusplus >= 201703L // DR 2686. /// std::hash specialization for error_condition. template<> @@ -411,7 +409,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(const error_condition& __e) const noexcept { const size_t __tmp = std::_Hash_impl::hash(__e.value()); - return std::_Hash_impl::__hash_combine(__e.category(), __tmp); + return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); } }; #endif diff --git a/libstdc++-v3/src/c++11/compatibility-c++0x.cc b/libstdc++-v3/src/c++11/compatibility-c++0x.cc index d57fdd23bcf..25ebe43e093 100644 --- a/libstdc++-v3/src/c++11/compatibility-c++0x.cc +++ b/libstdc++-v3/src/c++11/compatibility-c++0x.cc @@ -117,8 +117,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION size_t hash::operator()(error_code __e) const { - const size_t __tmp = std::_Hash_impl::hash(__e._M_value); - return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp); + const size_t __tmp = std::_Hash_impl::hash(__e.value()); + return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); } // gcc-4.7.0 diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc b/libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc new file mode 100644 index 00000000000..3bc05611418 --- /dev/null +++ b/libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc @@ -0,0 +1,51 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +#include +#include + +struct error_cat : std::error_category +{ + error_cat(std::string s) : _name(s) { } + std::string _name; + const char* name() const noexcept override { return _name.c_str(); } + std::string message(int) const override { return "error"; } +}; + +void +test01() +{ + std::hash h; + error_cat kitty("kitty"), moggy("moggy"); + std::error_condition cond1(99, kitty); + VERIFY( h(cond1) == h(cond1) ); + std::error_condition cond2(99, kitty); + VERIFY( h(cond1) == h(cond2) ); + std::error_condition cond3(88, kitty); + VERIFY( h(cond1) != h(cond3) ); + std::error_condition cond4(99, moggy); + VERIFY( h(cond1) != h(cond4) ); +} + +int +main() +{ + test01(); +} -- 2.30.2