Bug 13049 - Does not warn on obvious aliasing problem
Summary: Does not warn on obvious aliasing problem
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 3.4.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 20189 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-11-14 10:00 UTC by Andrew Pinski
Modified: 2021-09-06 08:00 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 3.3, 4.5.3, 4.8.3, 4.9.3, 5.3.0, 6.3.0, 7.0
Last reconfirmed: 2017-02-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2003-11-14 10:00:20 UTC
The following code should warn as there is an aliasing problem in the code (derived from gcc in 
SPEC, yes that old code):
struct tt
{
        struct
        {
          short i;
          short j;
        }t;
};
void t(struct tt *i)
{
  ((int*)i)[0] = 0;
  i->t.j = 2;
}
Comment 1 Andrew Pinski 2004-02-27 05:58:08 UTC
Mine as the reason why this does not happen is because &a->b is lowered so that there 
is only a cast.
Comment 2 Andrew Pinski 2004-04-05 00:53:14 UTC
Actually I was wrong, the reason why it does not warn is because we do not take the 
address so the warning is not going to be emitted at all.  I am not working on this any 
more.
Comment 3 Andrew Pinski 2004-06-20 20:47:11 UTC
But the reason why do not even think about warning has to do with the lowering part though.
Comment 4 Andrew Pinski 2005-02-26 14:26:44 UTC
*** Bug 20189 has been marked as a duplicate of this bug. ***
Comment 5 Andrew Pinski 2005-09-26 15:38:54 UTC
Even -Wstrict-aliasing=2 does not warn.
Comment 6 Martin Sebor 2017-02-28 03:30:42 UTC
The strict aliasing rule requires that

  An object shall have its stored value accessed only by an lvalue expression that has one of the following types
  ...

The test case in comment #0 doesn't involve accessing the stored value of an object so it doesn't violate those rules.

The following slightly modified version of the test case that does access the stored value of an object by an incompatible type and thus violate those rules is also not diagnosed, even by GCC 7.

$ cat t.c && gcc -O2 -S -Wall -Wextra -Wpedantic -Wstrict-aliasing=3 -fstrict-aliasing -fdump-tree-optimized=/dev/stdout t.c
struct tt
{
  struct { short i, j; } t;
};

int t (struct tt *i)
{
  i->t.i = 1;
  i->t.j = 2;
  return *(int*)i;   // violation here
}

;; Function int t(tt*) (_Z1tP2tt, funcdef_no=0, decl_uid=2282, cgraph_uid=0, symbol_order=0)

int t(tt*) (struct tt * i)
{
  int _5;

  <bb 2> [100.00%]:
  MEM[(short int *)i_2(D)] = 131073;
  _5 = MEM[(int *)i_2(D)];
  return _5;

}