From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id D3999385AC27 for ; Wed, 17 Nov 2021 00:32:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D3999385AC27 Received: by mail-pg1-x530.google.com with SMTP id r23so605226pgu.13 for ; Tue, 16 Nov 2021 16:32:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=3ZS5UPoW5qUfDxtvpD0R0LDhR7yeKvXdqNHsrY19W2w=; b=hebUP5BjQMUYY3vPGTZbatL8qZxDj81uv95eG7wnHc4MzfT4GobiP++gMd8DxzLr6h 8l1uScg3uYuk0txlJPuseCNqJNlJ9P2H9D+XiZI8DveLtrVJ4d/A8jfUXwodEN1Mi4AQ 97G8o/DdKh17UU58WbOFN/uCj9EiA83LoXo9RFxWT3/PVLIajtQ0+RN2p8VhBUGOxArw IRIxv20ZD83fS8cH75d7qs8wQ77slZ/hW5e0dsodPg/8RXj8xdW8Jj2l9jfH/cmeZc2N r07zQ1eD7egCXi98EIuhWSnneK/JkYyBu76xYHQ+/v01uxclc8maYDOHtQIoXlxbNjPX hz0g== X-Gm-Message-State: AOAM532vdUpV9Mu7lCWz4ZgZe4wggL/51sCB+nPxsHaGypOadXWg4Taq /EePxpnOVhHzYvMVg5xJOHqEwYmPAFg= X-Google-Smtp-Source: ABdhPJyTfwg6RRTQbiU+jKVPxOCAJpQ58QjOzeyxSlBWmf+/Qzn4nU7KacA6OYI1tI64qVM2j+D7ZQ== X-Received: by 2002:a63:c143:: with SMTP id p3mr2334819pgi.366.1637109122781; Tue, 16 Nov 2021 16:32:02 -0800 (PST) Received: from [192.168.0.41] (184-96-250-76.hlrn.qwest.net. [184.96.250.76]) by smtp.gmail.com with ESMTPSA id y32sm22429984pfa.145.2021.11.16.16.32.01 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 Nov 2021 16:32:02 -0800 (PST) To: gcc-patches From: Martin Sebor Subject: [PATCH] handle folded nonconstant array bounds [PR101702] Message-ID: <6ea3a876-d6d6-4103-bba7-2f4b14e74160@gmail.com> Date: Tue, 16 Nov 2021 17:32:00 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2BABCEBC0FF7F4587D08EF07" Content-Language: en-US X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Nov 2021 00:32:05 -0000 This is a multi-part message in MIME format. --------------2BABCEBC0FF7F4587D08EF07 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit -Warray-parameter and -Wvla-parameter assume that array bounds in function parameters are either constant integers or variable, but not something in between like a cast of a constant that's not recognized as an INTEGER_CST until we strip the cast from it. This leads to an ICE as the the internal checks fail. The attached patch fixes the problem by stripping the casts earlier than before, preventing the inconsistency. In addition, it also folds the array bound, avoiding a class of false positives and negatives that not doing so would lead to otherwise. Tested on x86_64-linux. Martin --------------2BABCEBC0FF7F4587D08EF07 Content-Type: text/x-patch; charset=UTF-8; name="gcc-101702.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-101702.diff" Handle folded nonconstant array bounds [PR101702] PR c/101702 - ICE: in handle_argspec_attribute, at c-family/c-attribs.c:3623 gcc/c/ChangeLog: PR c/101702 * c-decl.c (get_parm_array_spec): Strip casts earlier and fold array bounds before deciding if they're constant. gcc/testsuite/ChangeLog: PR c/101702 * gcc.dg/Warray-parameter-11.c: New test. diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 186fa1692c1..63d806a84c9 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -5866,6 +5866,12 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs) if (pd->u.array.static_p) spec += 's'; + if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts))) + /* Avoid invalid NELTS. */ + return attrs; + + STRIP_NOPS (nelts); + nelts = c_fully_fold (nelts, false, nullptr); if (TREE_CODE (nelts) == INTEGER_CST) { /* Skip all constant bounds except the most significant one. @@ -5883,13 +5889,9 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs) spec += buf; break; } - else if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts))) - /* Avoid invalid NELTS. */ - return attrs; /* Each variable VLA bound is represented by a dollar sign. */ spec += "$"; - STRIP_NOPS (nelts); vbchain = tree_cons (NULL_TREE, nelts, vbchain); } diff --git a/gcc/testsuite/gcc.dg/Warray-parameter-11.c b/gcc/testsuite/gcc.dg/Warray-parameter-11.c new file mode 100644 index 00000000000..8ca1b55bd28 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Warray-parameter-11.c @@ -0,0 +1,24 @@ +/* PR c/101702 - ICE on invalid function redeclaration + { dg-do compile } + { dg-options "-Wall" } */ + +typedef __INTPTR_TYPE__ intptr_t; + +#define copysign(x, y) __builtin_copysign (x, y) + +void f0 (double[!copysign (~2, 3)]); + +void f1 (double[!copysign (~2, 3)]); +void f1 (double[1]); // { dg-warning "-Warray-parameter" } + +void f2 (int[(int)+1.0]); +void f2 (int[(int)+1.1]); + +/* Also verify that equivalent expressions don't needlessly cause false + positives or negatives. */ +struct S { int a[1]; }; +extern struct S *sp; + +void f3 (int[(intptr_t)((char*)sp->a - (char*)sp)]); +void f3 (int[(intptr_t)((char*)&sp->a[0] - (char*)sp)]); +void f3 (int[(intptr_t)((char*)&sp->a[1] - (char*)sp)]); // { dg-warning "-Warray-parameter" } --------------2BABCEBC0FF7F4587D08EF07--