From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by sourceware.org (Postfix) with ESMTPS id E05163858018 for ; Wed, 20 Jan 2021 00:56:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E05163858018 Received: by mail-qk1-x730.google.com with SMTP id d14so23848513qkc.13 for ; Tue, 19 Jan 2021 16:56:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=8m9sWjix/XyPuLMiU5FZ/Fl29+puxYRLp5K8o6Si4+c=; b=WfJn4D0SXL74OhocOvNn8jgSL4FzGUDwExDkZjBC2MU/P18u+vF741nyXQlvjpUehU 6xrLf7ek3lgjiZdfAfWKqLB4bok60mw6NRNKy5FqFga9hVwOhhuxE5S+dkDHg4DcI8Ty nq8+JJO919Jaof0mz023XNSQA7DNsKOpJpVq3Af0JIuU1giceFcxMaGR44xHmWWL9pNx KdLB9VDxW6JFp7cmFUHL6JSampVw758z7sQumbyFiV7VYCVgApLXe9pnAXvuuCCj92jt 1GxhIWzrh3DObG9DhpxLU0Uk9+Au2zwgCvhGtfDSyq9dSXzqXlpwXTXbyaFE/Ggu9Cj6 cpAw== X-Gm-Message-State: AOAM530lBBN7esGFUP6R4niIp2ez+t07gbEtf4WRVDeqd5Unqn7Xlc0/ u267HJngJrY8cPQek5Z11XJDekyMpac= X-Google-Smtp-Source: ABdhPJylstGqcEcIEpTs/d2NUHHQwybFGNc84TBkuIdsizUaPucKJeEIvw5kjLhDAhhz5D2DtZRmqw== X-Received: by 2002:ae9:e119:: with SMTP id g25mr7381744qkm.124.1611104192163; Tue, 19 Jan 2021 16:56:32 -0800 (PST) Received: from [192.168.0.41] (184-96-239-30.hlrn.qwest.net. [184.96.239.30]) by smtp.gmail.com with ESMTPSA id m85sm332614qke.33.2021.01.19.16.56.31 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 19 Jan 2021 16:56:31 -0800 (PST) To: gcc-patches From: Martin Sebor Subject: [PATCH] avoid -Warray-bounds checks for vtable assignments (PR 98266) Message-ID: <7fa3c1e0-8a09-3436-8669-6d1a577ca247@gmail.com> Date: Tue, 19 Jan 2021 17:56:30 -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="------------AF9B6AE8F505055640653594" Content-Language: en-US X-Spam-Status: No, score=-10.8 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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, 20 Jan 2021 00:56:34 -0000 This is a multi-part message in MIME format. --------------AF9B6AE8F505055640653594 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Similar to the problem reported for -Wstringop-overflow in pr98266 and already fixed, -Warray-bounds is also susceptible to false positives in assignments and copies involving virtual inheritance. Because the two warnings don't share code yet (hopefully in GCC 12) the attached patch adds its own workaround for this problem to gimple-array-bounds.cc, this one slightly more crude because of the limited insight the array bounds checking has into the checked expressions. Tested on x86_64-linux. Martin --------------AF9B6AE8F505055640653594 Content-Type: text/x-patch; charset=UTF-8; name="gcc-98266.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-98266.diff" PR middle-end/98266 - bogus array subscript is partly outside array bounds on virtual inheritance gcc/ChangeLog: PR middle-end/98266 * gimple-array-bounds.cc (array_bounds_checker::check_array_bounds): Avoid checking references involving artificial members. gcc/testsuite/ChangeLog: PR middle-end/98266 * g++.dg/warn/Warray-bounds-15.C: New test. diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc index 2576556f76b..413deacece4 100644 --- a/gcc/gimple-array-bounds.cc +++ b/gcc/gimple-array-bounds.cc @@ -911,8 +911,16 @@ array_bounds_checker::check_array_bounds (tree *tp, int *walk_subtree, else if (TREE_CODE (t) == ADDR_EXPR) { checker->check_addr_expr (location, t); - *walk_subtree = FALSE; + *walk_subtree = false; } + else if (TREE_CODE (t) == COMPONENT_REF + && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF + && DECL_ARTIFICIAL (TREE_OPERAND (t, 1))) + /* Hack: Skip MEM_REF checking for artificial members to avoid false + positives for C++ classes with virtual bases. See pr98266 and + pr97595. */ + *walk_subtree = false; + /* Propagate the no-warning bit to the outer expression. */ if (warned) TREE_NO_WARNING (t) = true; diff --git a/gcc/testsuite/g++.dg/warn/Warray-bounds-15.C b/gcc/testsuite/g++.dg/warn/Warray-bounds-15.C new file mode 100644 index 00000000000..eb75527dc3d --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Warray-bounds-15.C @@ -0,0 +1,30 @@ +/* PR middle-end/98266 - bogus array subscript is partly outside array + bounds on virtual inheritance + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +#if __cplusplus < 201103L +# define noexcept throw () +#endif + +struct A +{ + virtual ~A () noexcept; + const char* s; +}; + +struct B: virtual A { }; +struct C: virtual B { }; +struct D: virtual A { }; // { dg-bogus "\\\[-Warray-bounds" } + +struct E: virtual B, virtual D +{ + E (const char*); +}; + +void f (E); + +void g () +{ + f (E ("")); +} --------------AF9B6AE8F505055640653594--