From: "H.J. Lu" <hjl.tools@gmail.com>
To: Bernd Edlinger <bernd.edlinger@hotmail.de>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
Jakub Jelinek <jakub@redhat.com>,
Jason Merrill <jason@redhat.com>
Subject: Re: [PATCH] Fix issues with -Waddress-of-packed-member
Date: Sun, 20 Jan 2019 15:41:00 -0000 [thread overview]
Message-ID: <CAMe9rOoEGpF-hrgr=16ZnCQmsixQP4rwubHXWV6Yc+YyAtTO_A@mail.gmail.com> (raw)
In-Reply-To: <AM0PR07MB3874860FEF4EF1C52FB1E6F7E49E0@AM0PR07MB3874.eurprd07.prod.outlook.com>
On Sun, Jan 20, 2019 at 5:29 AM Bernd Edlinger
<bernd.edlinger@hotmail.de> wrote:
>
> Hi,
>
>
> I tried to build linux yesterday, and became aware that there are a few
> false-positive warnings with -Waddress-of-packed-member:
>
> struct t {
> char a;
> int b;
> int *c;
> int d[10];
> } __attribute__((packed));
>
> struct t t0;
> struct t *t1;
> struct t **t2;
>
> t2 = &t1;
> i1 = t0.c;
>
> I fixed them quickly with the attached patch, and added a new test case,
> which also revealed a few missing warnings:
>
> struct t t100[10][10];
> struct t (*baz())[10];
>
> t2 = (struct t**) t100;
> t2 = (struct t**) baz();
>
>
> Well I remembered that Joseph wanted me to use min_align_of_type instead
> of TYPE_ALIGN in the -Wcast-align warning, so I changed -Waddress-of-packed-member
> to do that as well.
>
> Since I was not sure if the test coverage is good enough, I added a few more
> test cases, which just make sure the existing warning works properly.
You should also fix
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88928
with something like
diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
index 7821cc894a7..4daadc0f6c7 100644
--- a/gcc/c-family/c-warn.c
+++ b/gcc/c-family/c-warn.c
@@ -2744,9 +2744,14 @@ check_address_or_pointer_of_packed_member (tree type, tre
e rhs)
rhs = TREE_TYPE (rhs); /* Function type. */
}
tree rhstype = TREE_TYPE (rhs);
+ tree basetype = type;
+ while (POINTER_TYPE_P (basetype))
+ basetype = TREE_TYPE (basetype);
if ((POINTER_TYPE_P (rhstype)
|| TREE_CODE (rhstype) == ARRAY_TYPE)
- && TYPE_PACKED (TREE_TYPE (rhstype)))
+ && TYPE_PACKED (TREE_TYPE (rhstype))
+ && (!RECORD_OR_UNION_TYPE_P (basetype)
+ || TYPE_FIELDS (basetype) != NULL_TREE))
{
unsigned int type_align = TYPE_ALIGN_UNIT (type);
unsigned int rhs_align = TYPE_ALIGN_UNIT (TREE_TYPE (rhstype));
@@ -2759,7 +2764,8 @@ check_address_or_pointer_of_packed_member (tree
type, tree rhs)
"unaligned pointer value",
rhstype, rhs_align, type, type_align);
tree decl = TYPE_STUB_DECL (TREE_TYPE (rhstype));
- inform (DECL_SOURCE_LOCATION (decl), "defined here");
+ if (decl)
+ inform (DECL_SOURCE_LOCATION (decl), "defined here");
decl = TYPE_STUB_DECL (type);
if (decl)
inform (DECL_SOURCE_LOCATION (decl), "defined here");
> I am however not sure of a warning that is as noisy as this one, should be
> default-enabled, because it might interfere with configure tests. That might
These codes can lead unaligned access which may be very hard to track
down or fix since the codes in question may be in a library. I think it is a
very useful warning. Besides, these noisy warnings also reveal implementation
issues in GCC, which, otherwise, may not be known for a long time.
> be better to enable this warning, in -Wall or -Wextra, and, well maybe
> enable -Wcast-align=strict also at the same warning level, since it is currently
> not enabled at all, unless explicitly requested, what do you think?
>
>
> Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
> Is it OK for trunk?
>
>
--
H.J.
next prev parent reply other threads:[~2019-01-20 15:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-20 13:29 Bernd Edlinger
2019-01-20 15:41 ` H.J. Lu [this message]
2019-01-20 17:10 ` Bernd Edlinger
2019-01-20 18:26 ` H.J. Lu
2019-01-21 16:58 ` Jeff Law
2019-01-21 16:55 ` Jeff Law
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMe9rOoEGpF-hrgr=16ZnCQmsixQP4rwubHXWV6Yc+YyAtTO_A@mail.gmail.com' \
--to=hjl.tools@gmail.com \
--cc=bernd.edlinger@hotmail.de \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).