From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id E1D563858408 for ; Mon, 20 Nov 2023 02:34:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E1D563858408 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E1D563858408 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::536 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700447668; cv=none; b=iDqlJgq4RH8HQIfV+1aq5O0DeaGxVuS0kgoOyy/YRVl1SfPkNB5TB0/4eUimsDtl4MXnUnKCaqeENqCgG4xctEfz/n2jIRw+jF4rfsRxdO7bh0vc90tcl9hKFdjvXQsNhbv+z81n89sYKGuBSVsytatMtCaGvg0IXDI4Bjswnr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700447668; c=relaxed/simple; bh=dsQOkIRnZgSURqpr3Fg0kByIDvc8dmM7LnDZFZxB1G4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=c4YdK32nyBt2LY2+QsJbNz+gsGj5tHwuhsvrdJ1/JVNz0rO87h6IPwY6n3IcEj2L4ERHxZknT6zChe3b3NqA1ogzyodCJmr1pqhdkA8ASn9RHygducEJ5CRKilC0y+Dwk6xFjsQ33mm7xPgtgOcQahIx8qH0p3e68RH6BlB58p8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-5c210e34088so1722514a12.2 for ; Sun, 19 Nov 2023 18:34:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1700447666; x=1701052466; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=2whmJuI9ji6BMZQ+KWl5c3LnOC4JF5HSsMdmqe+HUc0=; b=JesJQZRMoHTqXe/m5L0qTw9HxTIlyyjrSuG7DOcHxvtuZg1g0zZTPTa4i458MhG6Ac Y0XeFQMFXecpNTZ0xz9Y+qfokERlyI5kXxmPMsIDi+vg559mKVwWnf6bKe9BDGzDYwbe S61Rbq217ZuIWRRe2yI6ELHSrbux/zRNBaBFUjyHwxd84BjdXACp7ArgW+S6NjS1/F2a LJHfjUBLG1EXthE22nYlTs2h4TpXOOyk6HpJWW/9O5oZMCOIKC3/KbF0OwNQdalz5lyN UhraSk01u+khInKSeJlRaysKt1QwkL4EzEfOR2q0Ty8UUckzXExRfkGRYPY+MMz3XEPO fTnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700447666; x=1701052466; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2whmJuI9ji6BMZQ+KWl5c3LnOC4JF5HSsMdmqe+HUc0=; b=eULfi3mKrYUfYDJ/EHXWa5y54NkIXtlgSE0p1HM4jcMKBYk5GQ5YfozJlM/rPwDGqp jsSvwGw0lM3oHEiuoAhE6ozXDwetD/KHCey/4sg0AKGqherqtR4jffNy1LAS011wTe6L FagGFn4OobYosuoYUPmeM3H4nngibGPm/Miv4CLFFIEfXyogWE1alMXDB03uG1X/xx11 pfheiTS4zeQGo2sJyja6kMFQba/3tqM9xUQiWQGao2hFTY7iKXnz31y7Wm2jyPRKyy5b d9bwACS9pJbiBD6lTsmExfvYwW/RKbX1sU9GDYDLhnXVhd9Zqhe4Es877i7IiB5EDf16 qo3A== X-Gm-Message-State: AOJu0Yx9kf4pdjxWx9fLs11Wq6sDh/sGHsd/LZkC7/XoSa6l9foYkC0H 0iOp8ozEiGbnjj///DhMd3J4dkep3pTEtAYFM9E= X-Google-Smtp-Source: AGHT+IHazqPdOed+CqkbKSk3onQUHcd1s1Lc1+TcDS94RsCnJZ38H7GDr/n32XgOgib6v56DQmGUVA== X-Received: by 2002:a05:6a21:398d:b0:188:aea:2138 with SMTP id ad13-20020a056a21398d00b001880aea2138mr8462378pzc.19.1700447665899; Sun, 19 Nov 2023 18:34:25 -0800 (PST) Received: from free.home ([2804:7f1:2080:51b3:d545:cbee:40c0:c7a7]) by smtp.gmail.com with ESMTPSA id 5-20020a17090a1a0500b00267d9f4d340sm7204994pjk.44.2023.11.19.18.34.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 18:34:25 -0800 (PST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 3AK2YHfe146683 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sun, 19 Nov 2023 23:34:17 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Cc: David Malcolm , Rainer Orth , Mike Stump , Jason Merrill , Nathan Sidwell Subject: Re: [PATCH #3/4] warn on cast of pointer to packed plus constant Organization: Free thinker, does not speak for AdaCore References: Date: Sun, 19 Nov 2023 23:34:17 -0300 In-Reply-To: (Alexandre Oliva's message of "Sun, 19 Nov 2023 04:36:23 -0300") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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. Regstrapped on x86_64-linux-gnu, also tested on arm-eabi with default cpu on trunk, and with tms570 on gcc-13. Ok to install? 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. --- 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 d2938b91043d3..2ef73d7088f22 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); -- Alexandre Oliva, happy hacker https://FSFLA.org/blogs/lxo/ Free Software Activist GNU Toolchain Engineer More tolerance and less prejudice are key for inclusion and diversity Excluding neuro-others for not behaving ""normal"" is *not* inclusive