From patchwork Mon Jan 29 18:12:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 867249 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-472196-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Kcmm65dC"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zVd0p60THz9s7M for ; Tue, 30 Jan 2018 05:13:13 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=UJx0nBStfar6CE4iYsDAdUkbDQN/0xL9Nr8yeqILGuBkOZS5BxNE2 +fnz2M5QO13pL/VXPqUcOReC14oKQOnvhNlUxwfgrzqmgi0K1/EVZbhX5FW3CxUW 1U43kiys7iqM5bLJi3BpJqw94yirrvsfwH6ykRCmJiWqhh92fjP8ec= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=FRXvJ4lrj6kLT5G90/cH72+oGvE=; b=Kcmm65dCdEZbXHlnBm+m 2YB23XXl2I52Bero1wBnez6TYyUs1XnZ40CmbeLwzNZkHimf04wHAZOa/ZQ60tbx u+537Mcst45OJeJlU8pDLvGfa96rbnZQhmPx8V24D7gk121Y0E02F3fTYNXetLRH WKZ3a3qo2rQVdvTHR9ttOCE= Received: (qmail 26214 invoked by alias); 29 Jan 2018 18:13:06 -0000 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 Received: (qmail 26205 invoked by uid 89); 29 Jan 2018 18:13:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 29 Jan 2018 18:13:04 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C8EBFC057EC3 for ; Mon, 29 Jan 2018 18:13:03 +0000 (UTC) Received: from redhat.com (unknown [10.40.205.80]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D9CEC1823F; Mon, 29 Jan 2018 18:13:02 +0000 (UTC) Date: Mon, 29 Jan 2018 19:12:57 +0100 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: C++ PATCH to fix a crash in cxx_fold_indirect_ref (PR c++/83996) Message-ID: <20180129181257.GG2620@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.1 (2017-09-22) The crash here is caused by size_binop_loc getting operands of different types: sizetype and ssizetype. Fixed by performing the computation in offset_int, much as we do in fold_indirect_ref_1 (fixed in middle-end/81695). Bootstrapped/regtested on x86_64-linux, ok for trunk? 2018-01-29 Marek Polacek PR c++/83996 * constexpr.c (cxx_fold_indirect_ref): Compute ((foo *)&fooarray)[1] => fooarray[1] in offset_int. * g++.dg/ext/pr83996.C: New test. Marek diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c index 4d2ee4a28fc..0a1944f6dad 100644 --- gcc/cp/constexpr.c +++ gcc/cp/constexpr.c @@ -3143,11 +3143,17 @@ cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base) tree min_val = size_zero_node; if (type_domain && TYPE_MIN_VALUE (type_domain)) min_val = TYPE_MIN_VALUE (type_domain); - op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01, - TYPE_SIZE_UNIT (type)); - op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val); - return build4_loc (loc, ARRAY_REF, type, op00, op01, - NULL_TREE, NULL_TREE); + offset_int off = wi::to_offset (op01); + offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type)); + offset_int remainder; + off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder); + if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST) + { + off = off + wi::to_offset (min_val); + op01 = wide_int_to_tree (sizetype, off); + return build4_loc (loc, ARRAY_REF, type, op00, op01, + NULL_TREE, NULL_TREE); + } } /* Also handle conversion to an empty base class, which is represented with a NOP_EXPR. */ diff --git gcc/testsuite/g++.dg/ext/pr83996.C gcc/testsuite/g++.dg/ext/pr83996.C index e69de29bb2d..e663d728349 100644 --- gcc/testsuite/g++.dg/ext/pr83996.C +++ gcc/testsuite/g++.dg/ext/pr83996.C @@ -0,0 +1,11 @@ +// PR c++/83996 +// { dg-do compile } +// { dg-options "" } + +int z[] = { }; + +int +main (void) +{ + __builtin_printf ("%d\n", *(z + 1)); +}