From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id B563A385DC30; Thu, 23 Nov 2023 11:46:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B563A385DC30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700740003; bh=Nveoib+gV43p+QhFbdrOvMpCEnTH27fhG1E6D5X2Imk=; h=From:To:Subject:Date:From; b=yZz2gN460PYloIZXnPG1YFlnaDIMNzbKmWBNBk9ZtwAfSd910HrTRJ6KgHRtNkyvk gCMSofSbouckbJkSmvbW/VuouwJy/UvU9o0a49MnHthZZf8NRqUzwPa+9hTPAlXoy/ 8ynEttNM9NbAFX5ekYYow0Q2BQHU0xtkssNFUYng= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/aoliva/heads/testme)] warn on cast of pointer to packed plus constant X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: fd0ca8cd52e74ee5dbb04aecc7d625cc430c55a2 X-Git-Newrev: cbcac1b2f5f54c8a85c42f38233271ddaecb9a36 Message-Id: <20231123114643.B563A385DC30@sourceware.org> Date: Thu, 23 Nov 2023 11:46:43 +0000 (GMT) List-Id: https://gcc.gnu.org/g:cbcac1b2f5f54c8a85c42f38233271ddaecb9a36 commit cbcac1b2f5f54c8a85c42f38233271ddaecb9a36 Author: Alexandre Oliva Date: Sun Nov 19 01:25:01 2023 -0300 warn on cast of pointer to packed plus constant c-c++-common/analyzer/null-deref-pr108251-smp-fetch_ssl_fc_has_early.c gets an unaligned pointer value warning on -fshort-enums targets in C, but not in C++. The former simplifies the offset-and-cast expression enough that check_and_warn_address_or_pointer_of_packed_member finds no more than a type cast of the base pointer, but in C++, the entire expression, with cast, constant offsetting, and cast again, is retained, and that's too much for the warning code. Or rather it was. It's easy enough to take the base pointer from PLUS_POINTER_EXPR, and a constant offset can't possibly increase alignment for just any pointer of laxer alignment, so we can safely disregard the offset. This should improve the warning even in C, if the packed enum is at a nonzero offset into the containing struct. for gcc/c-family/ChangeLog * c-warn.cc (check_and_warn_address_or_pointer_of_packed_member): Take the base pointer from PLUS_POINTER_EXPR when the addend is constant. Diff: --- gcc/c-family/c-warn.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc index d2938b91043..2ef73d7088f 100644 --- a/gcc/c-family/c-warn.cc +++ b/gcc/c-family/c-warn.cc @@ -3108,10 +3108,20 @@ check_and_warn_address_or_pointer_of_packed_member (tree type, tree rhs) do { - while (TREE_CODE (rhs) == COMPOUND_EXPR) - rhs = TREE_OPERAND (rhs, 1); - orig_rhs = rhs; + do + { + orig_rhs = rhs; + while (TREE_CODE (rhs) == COMPOUND_EXPR) + rhs = TREE_OPERAND (rhs, 1); + /* Constants can't increase the alignment. */ + while (TREE_CODE (rhs) == POINTER_PLUS_EXPR + && TREE_CONSTANT (TREE_OPERAND (rhs, 1))) + rhs = TREE_OPERAND (rhs, 0); + } + while (orig_rhs != rhs); STRIP_NOPS (rhs); + while (TREE_CODE (rhs) == VIEW_CONVERT_EXPR) + rhs = TREE_OPERAND (rhs, 0); nop_p |= orig_rhs != rhs; } while (orig_rhs != rhs);