From patchwork Mon Jan 2 13:25:40 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 133838 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 531051007D2 for ; Tue, 3 Jan 2012 00:27:15 +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=1326115637; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Message-ID:Date:From:User-Agent:MIME-Version: To:CC:Subject:References:In-Reply-To:Content-Type:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=tZ/hGGYgnnWOryKE1Wv9Qeu1m9U=; b=RO6qBPRw0ZJLaSTjj2jlOr2Je3bjjVX8zcEEI3B2Kz0O/k+izu3hFVuB8FywFr kSIfvZ4nEvtbUgxUm4xTOjd0b+cDV6IMwqcbz7HDZCod1sP96Y8bzc7n3Y75GDQ9 i7nZeF/0f6EbwarA2vUPfRxP6n+tvgaJCE8WJmK5Hck00= 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:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=eG8r86lcUjU4XEXADpElXPJCfC9BjdLpijCHMQetoaZQRZNn6C8oudsAdkg58P BGN+Ly9e/MVUDvU6C4AOWcBjFUAiIAXV9fy8M59e6nhLKvt+dl6Fii6bBkhKXUQB mPK1WyYqpTHUZ6maHaVGtRjXxoxEL2IiFYSv6R5MdWm2U=; Received: (qmail 28014 invoked by alias); 2 Jan 2012 13:27:10 -0000 Received: (qmail 28006 invoked by uid 22791); 2 Jan 2012 13:27:09 -0000 X-SWARE-Spam-Status: No, hits=-3.2 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, TW_CP X-Spam-Check-By: sourceware.org Received: from acsinet15.oracle.com (HELO acsinet15.oracle.com) (141.146.126.227) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 02 Jan 2012 13:26:56 +0000 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by acsinet15.oracle.com (Switch-3.4.4/Switch-3.4.4) with ESMTP id q02DQsRi015931 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 2 Jan 2012 13:26:55 GMT Received: from acsmt358.oracle.com (acsmt358.oracle.com [141.146.40.158]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id q02DQr3w008729 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 2 Jan 2012 13:26:54 GMT Received: from abhmt104.oracle.com (abhmt104.oracle.com [141.146.116.56]) by acsmt358.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id q02DQrJL030143; Mon, 2 Jan 2012 07:26:53 -0600 Received: from [192.168.1.4] (/79.53.82.186) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 02 Jan 2012 05:26:53 -0800 Message-ID: <4F01B054.6070809@oracle.com> Date: Mon, 02 Jan 2012 14:25:40 +0100 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111219 Thunderbird/9.0 MIME-Version: 1.0 To: Richard Guenther CC: Jason Merrill , "gcc-patches@gcc.gnu.org" Subject: Re: [RFH / Patch] PR 20140 References: <4F01040A.8050803@oracle.com> <4F010D14.6020304@redhat.com> <4F011549.2040602@oracle.com> In-Reply-To: X-IsSubscribed: yes 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 Hi, > On Mon, Jan 2, 2012 at 3:24 AM, Paolo Carlini wrote: >> On 01/02/2012 02:49 AM, Jason Merrill wrote: >>> On 01/01/2012 08:10 PM, Paolo Carlini wrote: >>>> The analysis is confirmed by the fact that the rather heavy handed >>>> patchlet which I'm attaching, which uses copy_node to avoid the >>>> corruption, "works". How do we normally handle this kind of situation? >>> In most cases, trees are unshared at instantiation time, but that doesn't >>> apply to constants like this. The patch is OK. >> Oh I see, thanks. Then I'm going to properly test it, etc, and if everything >> goes well, I'll post what I will actually commit. > I think it's enough to call copy_node for CONSTANT_CLASS_P objects. In this branch we are always dealing with STRING_CST as init, I don't think we are going to discriminate much by using that predicate. Or maybe I'm not getting your point. Anyway, in the meanwhile I investigated the issue somewhat more, and my tentative fix indeed works but it's definitely a too big hammer in terms of memory management and memcpy for no reason: we would copy_node very often, also when a plain char array is initialized with a plain char string literal, or a wchar_t from a wchar_t. Bad, because currently many circumstances in which we do TREE_TYPE (init) = type are in fact benign: char - char and wchar_t - wchar_t definitely work unconditionally (and within an individual instantiation we don't have problems, only between instantiations). Thus I'm thinking the below would be better (and safe), copy_node should trigger much less often, essentially, only when an explicitly unsigned / signed char array is involved. Tested x86_64-linux. Thanks, Paolo. ///////////////////////// /cp 2012-01-02 Paolo Carlini PR c++/20140 * typeck2.c (digest_init_r): Use copy_init when initializing unsigned and signed char arrays. /testsuite 2012-01-02 Paolo Carlini PR c++/20140 * g++.dg/template/init9.C: New. Index: testsuite/g++.dg/template/init9.C =================================================================== --- testsuite/g++.dg/template/init9.C (revision 0) +++ testsuite/g++.dg/template/init9.C (revision 0) @@ -0,0 +1,12 @@ +// PR c++/20140 + +template void foo() +{ + unsigned char s[] = ""; +} + +int main() +{ + foo(); + foo(); +} Index: cp/typeck2.c =================================================================== --- cp/typeck2.c (revision 182784) +++ cp/typeck2.c (working copy) @@ -902,6 +902,8 @@ digest_init_r (tree type, tree init, bool nested, } } + if (char_type != typ1) + init = copy_node (init); TREE_TYPE (init) = type; if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type))) {