From patchwork Mon Jan 28 02:02:33 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 216102 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 2A2912C0092 for ; Mon, 28 Jan 2013 13:03:05 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1359943386; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Date:Message-ID:Subject:From:To: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=E828QkL ykbTZlnDzh5jaBzSoou8=; b=yWykytpTlv3TR9CxYWu9KkOupJIHk6ddB9NXXmf 6dQdJKyTuWZLx+w+oUreTdtviovxAYMMaRZm5OpyR9ezBSxL6gwOXDyGzEekdlDI aYDWkjS/wQN60W72lL5djFWuVTKvUggJ0P0RcLH98NQ19X+ajiUuppVocj+o3orL lK/A= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:MIME-Version:X-Received:Received:Date:Message-ID:Subject:From:To:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=FxgZThdxlTSNSKMopX0VUIHQhEZYw8gQV0jBfliIOtOypCo99osEPbMixwQMGS DBQRtkOfpj/sdD7jZLaFbgmFXPIJ2Ur+ffEPRIVcFymRCkrIgQxPuuQ9MG0eq7ZZ 9MEZIsIInAfX3c1a3agWaighEnxgvxICVVNHwVB8VPxME=; Received: (qmail 20856 invoked by alias); 28 Jan 2013 02:02:41 -0000 Received: (qmail 20832 invoked by uid 22791); 28 Jan 2013 02:02:39 -0000 X-SWARE-Spam-Status: No, hits=-4.5 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE X-Spam-Check-By: sourceware.org Received: from mail-lb0-f169.google.com (HELO mail-lb0-f169.google.com) (209.85.217.169) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 28 Jan 2013 02:02:35 +0000 Received: by mail-lb0-f169.google.com with SMTP id m4so3277896lbo.0 for ; Sun, 27 Jan 2013 18:02:33 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.152.122.133 with SMTP id ls5mr11780769lab.9.1359338553194; Sun, 27 Jan 2013 18:02:33 -0800 (PST) Received: by 10.112.125.71 with HTTP; Sun, 27 Jan 2013 18:02:33 -0800 (PST) Date: Mon, 28 Jan 2013 02:02:33 +0000 Message-ID: Subject: [patch] Fix libstdc++/56112, inserting non-pairs into std::unordered_map From: Jonathan Wakely To: "libstdc++" , gcc-patches Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This is a much simpler fix than discussed in bugzilla, just forward to _M_emplace() when inserting from a type that needs conversion to value_type. This also fixes the dangling reference problem as well as the regression with calling insert() with convertible types. PR libstdc++/56112 * include/bits/hashtable_policy.h (insert(_Pair&&)): Use _M_emplace to construct value_type explicitly before trying to extract the key. * testsuite/23_containers/unordered_map/cons/56112.cc: New. Tested x86_64-linux, will commit to trunk tomorrow. N.B. The dangling reference bug is present on the 4.6 and 4.7 branches, but not a regression. commit ca741fccc5d052db08ba839bd45ece7564ac5004 Author: Jonathan Wakely Date: Mon Jan 28 00:08:43 2013 +0000 PR libstdc++/56112 * include/bits/hashtable_policy.h (insert(_Pair&&)): Use _M_emplace to construct value_type explicitly before trying to extract the key. * testsuite/23_containers/unordered_map/cons/56112.cc: New. diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h index 023f46d..1ade3f9 100644 --- a/libstdc++-v3/include/bits/hashtable_policy.h +++ b/libstdc++-v3/include/bits/hashtable_policy.h @@ -836,7 +836,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION insert(_Pair&& __v) { __hashtable& __h = this->_M_conjure_hashtable(); - return __h._M_insert(std::forward<_Pair>(__v), __unique_keys()); + return __h._M_emplace(__unique_keys(), std::forward<_Pair>(__v)); } template> diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/56112.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/56112.cc new file mode 100644 index 0000000..c297ef7 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/56112.cc @@ -0,0 +1,49 @@ +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2013 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 +// . + +#include +#include + +struct Key +{ + explicit Key(const int* p) : value(p) { } + ~Key() { value = nullptr; } + + bool operator==(const Key& k) const { return *value == *k.value; } + + const int* value; +}; + +struct hash +{ + std::size_t operator()(const Key& k) const noexcept { return *k.value; } +}; + +struct S +{ + int value; + operator std::pair() const { return {Key(&value), value}; } +}; + +int main() +{ + S s[1] = { {2} }; + std::unordered_map m(s, s+1); + std::unordered_multimap mm(s, s+1); +}