public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mikpe at it dot uu dot se" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug other/41809]  New: escaping address of packed field should trigger warning
Date: Fri, 23 Oct 2009 16:06:00 -0000	[thread overview]
Message-ID: <bug-41809-7665@http.gcc.gnu.org/bugzilla/> (raw)

A field in a packed struct will often not have the normal alignment for its
type.  So taking the address of such a field may yield a misaligned pointer,
but gcc does not warn about that.  This is similar to casting a pointer from a
less aligned type to a more aligned type, which does give a warning (with
-Wcast-align).

Example:
> cat aopf.c
#include <stdio.h>

void __attribute__((noinline)) f(int *p)
{
    printf("%p\n", p);
}

struct s {
    int x;
    char c;
} __attribute__((__packed__));

struct s A[10];

int main(void)
{
    unsigned int i;

    for (i = 0; i < sizeof(A)/sizeof(A[0]); ++i) {
        f(&A[i].x);
        f((int*)(char*)&A[i].x);
    }    
    return 0;
}
> gcc -Wall -Wextra -Wcast-align -O aopf.c
aopf.c: In function 'main':
aopf.c:21:4: warning: cast increases required alignment of target type

Ideally both calls to f() should trigger alignment warnings.


-- 
           Summary: escaping address of packed field should trigger warning
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: other
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mikpe at it dot uu dot se


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41809


             reply	other threads:[~2009-10-23 16:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-23 16:06 mikpe at it dot uu dot se [this message]
2009-10-26 21:47 ` [Bug other/41809] " hp at gcc dot gnu dot org

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=bug-41809-7665@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).