Bug 60581 - gcc doesn't isssue a strict aliasing warning on a code that seems to break it
Summary: gcc doesn't isssue a strict aliasing warning on a code that seems to break it
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.7.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: alias, diagnostic
Depends on:
Blocks:
 
Reported: 2014-03-19 09:07 UTC by Rafał Mużyło
Modified: 2014-06-30 19:31 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-03-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rafał Mużyło 2014-03-19 09:07:48 UTC
The problem is described here: https://bugs.gentoo.org/show_bug.cgi?id=505026
The code to trigger comes from a launchpad bug: https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1072650

As you may notice, it's acros distros and affects many compiler versions (confirmed for 4.6.3 on ubuntu, 4.7.3 and 4.8.2 on Gentoo).

Following code (AFAICT) violates strict aliasing rules:
#include <stdio.h>

struct psuedo_hdr
{
  int saddr;
  int daddr;
  char zero;
  char protocol;
  short len;
} __attribute__((packed));

int main()
{
  unsigned int i;
  unsigned int sum = 0;
  struct psuedo_hdr hdr;

  hdr.saddr = 0xaabbccdd;
  hdr.daddr = 0x11223344;
  hdr.zero = 0;
  hdr.protocol = 6;
  hdr.len = 2;
  for (i = 0; i < sizeof(hdr); i += 2)
    sum += *(short *)((char *)(&hdr) + i);
  printf("0x%x\n", sum);
  return 0;
}

however, '-O2 -Wall' doesn't result in the strict aliasing warning.
Comment 1 Richard Biener 2014-03-19 10:00:27 UTC
The strict-aliasing warnings are broken - they are too easily to silence (the (char *) cast for example).  Generally warning for TBAA violations is very hard if you want to avoid gazillions of false positives or gazillions of false negatives.  The present warning code delivers neither :/