public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/16197] New: CMSG_DATA results in (possibly correct) string aliasing warnings on gcc
@ 2013-11-20 21:31 luto at mit dot edu
  2013-11-21  0:26 ` [Bug libc/16197] " bugdal at aerifal dot cx
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: luto at mit dot edu @ 2013-11-20 21:31 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=16197

            Bug ID: 16197
           Summary: CMSG_DATA results in (possibly correct) string
                    aliasing warnings on gcc
           Product: glibc
           Version: 2.17
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: luto at mit dot edu
                CC: drepper.fsp at gmail dot com

Using CMSG_DATA on most gcc configurations gives a string aliasing warning. 
This happens because struct cmsg_hdr is:

struct cmsghdr
{
    size_t cmsg_len;
    int cmsg_level;
    int cmsg_type;
    __extension__ unsigned char __cmsg_data [];
};

GCC thinks that __cmsg_data is an array of objects of type unsigned char, in
which case it's illegal to access them through a pointer to anything else (as
CMSG_DATA does).  I'm not sure whether it's legal to put objects of, for
example, type int into storage defined by an array of type unsigned char, but
I'm not particularly surprised that gcc warns.

Anything like this:

    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr);
    if (cmsg) {
        /* This is IPPROTO_IP / IP_TTL */
        if (cmsg->cmsg_level == 0 && cmsg->cmsg_type == 2) {
            use(*(int *)CMSG_DATA(cmsg));
        }
    }

will trigger the warning if built with -Wall -O2.

There's a trivial fix: stop using flexible arrays.  The pre-C99
non-gcc-extension-using variant (which is already in bits/socket.h) is fine.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-08-27 22:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-20 21:31 [Bug libc/16197] New: CMSG_DATA results in (possibly correct) string aliasing warnings on gcc luto at mit dot edu
2013-11-21  0:26 ` [Bug libc/16197] " bugdal at aerifal dot cx
2013-11-21  5:32 ` luto at mit dot edu
2013-12-02  6:45 ` crrodriguez at opensuse dot org
2014-06-13 11:59 ` fweimer at redhat dot com
2015-08-27 22:18 ` [Bug network/16197] " jsm28 at gcc dot gnu.org

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).