Bug 14024 - g++ isn't reporting aliasing warnings
Summary: g++ isn't reporting aliasing warnings
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 enhancement
Target Milestone: 4.2.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 23915 (view as bug list)
Depends on:
Blocks: 21920
  Show dependency treegraph
 
Reported: 2004-02-05 04:48 UTC by Timothy J. Wood
Modified: 2005-12-07 16:38 UTC (History)
5 users (show)

See Also:
Host: *-*-*
Target: *-*-*
Build: *-*-*
Known to work: 4.2.0
Known to fail: 3.3.3 3.4.0 3.4.4 4.0.0 4.1.0
Last reconfirmed: 2005-06-29 04:05:10


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Timothy J. Wood 2004-02-05 04:48:22 UTC
int test(int input)
{
    float *f = (float *)&input;
    return input;
}


When built with gcc:

$PREFIX/bin/gcc -Wstrict-aliasing -fstrict-aliasing -c alias.c
/tmp/alias.c: In function `test':
/tmp/alias.c:3: warning: dereferencing type-punned pointer will break strict-aliasing rules

But, when the same input is built with g++, no warning is issued.  This is bad since -fstrict-aliasing 
does seem to be taking effect (i.e., my real program produces crazy output with -fstrict-aliasing 
on), but no warnings are generating, meaning that it will be potentially very hard to track down the 
issue.
Comment 1 Wolfgang Bangerth 2004-02-05 15:59:46 UTC
Confirmed. Unfortunate indeed. 
 
W. 
Comment 2 Uroš Bizjak 2004-12-29 07:49:22 UTC
This example is from PR 16111:
#include <stdio.h>

float foo ()
{
  unsigned int MASK = 0x80000000;
  return (float &) MASK;
}

int main() {
  printf ("%f\n", foo());
  return 0;
}

'g++ -Wstrict-aliasing' does not generate a warning about breaking
strict-aliasing rules. When '(float&)MASK' is replaced with equivalent '*(float
*)&MASK', 'gcc -Wstrict-aliasing' produces expected warning.
Comment 3 Andrew Pinski 2005-09-16 14:15:04 UTC
*** Bug 23915 has been marked as a duplicate of this bug. ***
Comment 4 Richard Biener 2005-11-24 10:48:18 UTC
Subject: Bug 14024

Author: rguenth
Date: Thu Nov 24 10:48:15 2005
New Revision: 107459

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=107459
Log:
2005-11-24  Richard Guenther  <rguenther@suse.de>
	Dirk Mueller <dmueller@suse.de>

	PR c++/14024
	* c-common.h (strict_aliasing_warning): Declare.
	* c-common.c (strict_aliasing_warning): New function,
	split out from ...
	* c-typeck.c (build_c_cast): ... here.

	* typeck.c (build_reinterpret_cast_1): Use it.

	* g++.dg/warn/Wstrict-aliasing-1.C: New testcase.
	* g++.dg/warn/Wstrict-aliasing-2.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-3.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-4.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-5.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-6.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-1.C
    trunk/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-2.C
    trunk/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-3.C
    trunk/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-4.C
    trunk/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-5.C
    trunk/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-6.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-common.c
    trunk/gcc/c-common.h
    trunk/gcc/c-typeck.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog

Comment 5 Richard Biener 2005-11-24 10:55:51 UTC
Fixed in 4.2.
Comment 6 Jack Howarth 2005-12-07 16:14:36 UTC
Can we please have the fix for PR 14024 backported into gcc 4.1? It will be very difficult
to get upstream packages like swig for c++ fixed with regards to the dereferencing type-punned
pointers breaking strict-aliasing if I have to force them to use an unstable trunk version of
gcc to do so.
Comment 7 Andrew Pinski 2005-12-07 16:19:48 UTC
(In reply to comment #6)
> Can we please have the fix for PR 14024 backported into gcc 4.1? It will be
> very difficult to get upstream packages like swig for c++ fixed with regards to the
> dereferencing type-punned
> pointers breaking strict-aliasing if I have to force them to use an unstable
> trunk version of gcc to do so.

Why?  Most of the places where aliasing violations happen are very obvious (see the example in the PR).
You just need to go through every source and audit it (which should happen for overflows and all other undefined behaviors).  The warning is only for helping and nothing else.  There are a lot of other undefined behaviors which GCC does not warn for which most people forget to look for.  One is signed overflow being undefined.  If we warn for that, almost every single line would warn as a = b+c; could overflow.  There are many more.
Comment 8 Dirk Mueller 2005-12-07 16:33:54 UTC
that comparison isn't quite fair. strict aliasing is an optimisation that breaks code when compiled with a newer version of gcc, and there is lots of code to fix because of that. 

Sure, you can fix it by reading through all of the source code and debugging where a particular failure comes from, but its easier to recompile with Wall and grep the build log. 

Comment 9 Andrew Pinski 2005-12-07 16:38:27 UTC
(In reply to comment #8)
> that comparison isn't quite fair. strict aliasing is an optimisation that
> breaks code when compiled with a newer version of gcc, and there is lots of
> code to fix because of that. 

Actually it is.  Since GCC now uses the fact that signed types overflow is undefined more than before.
I know since I was the one who made GCC use that fact more.  (there are even more going in for 4.2).
Aliasing is not that much different as every one who writes C or C++ should know the rules about aliasing and signed overflow but most don't.  Though aliasing violations is the most reported bug, we still get bugs about signed overflow.  Both have a way to turn off the optimization that uses the facts that the standard provides.  Though both make the code really invalid C++.
Comment 10 Richard Biener 2005-12-08 11:24:17 UTC
Subject: Bug 14024

Author: rguenth
Date: Thu Dec  8 11:24:07 2005
New Revision: 108226

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=108226
Log:
2005-12-08  Richard Guenther  <rguenther@suse.de>

        Backport from mainline
        2005-11-28  Richard Guenther  <rguenther@suse.de>

	* c-common.c (strict_aliasing_warning): Handle all
	component-ref like accesses.

	* gcc.dg/alias-9.c: New testcase.
	* g++.dg/warn/Wstrict-aliasing-7.C: Likewise.

        2005-11-24  Richard Guenther  <rguenther@suse.de>
	        Dirk Mueller <dmueller@suse.de>

	PR c++/14024
	* c-common.h (strict_aliasing_warning): Declare.
	* c-common.c (strict_aliasing_warning): New function,
	split out from ...
	* c-typeck.c (build_c_cast): ... here.

	* typeck.c (build_reinterpret_cast_1): Use
	strict_aliasing_warning.

	* g++.dg/warn/Wstrict-aliasing-1.C: New testcase.
	* g++.dg/warn/Wstrict-aliasing-2.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-3.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-4.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-5.C: Likewise.
	* g++.dg/warn/Wstrict-aliasing-6.C: Likewise.

        2005-11-23  Paolo Carlini  <pcarlini@suse.de>

	PR libstdc++/24975 (basic_string)
	* include/bits/basic_string.h (_Rep::_S_empty_rep): Avoid
	strict-aliasing warnings.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-1.C
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-2.C
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-3.C
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-4.C
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-5.C
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-6.C
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-7.C
    branches/gcc-4_1-branch/gcc/testsuite/gcc.dg/alias-9.c
Modified:
    branches/gcc-4_1-branch/gcc/ChangeLog
    branches/gcc-4_1-branch/gcc/c-common.c
    branches/gcc-4_1-branch/gcc/c-common.h
    branches/gcc-4_1-branch/gcc/c-typeck.c
    branches/gcc-4_1-branch/gcc/cp/ChangeLog
    branches/gcc-4_1-branch/gcc/cp/typeck.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_1-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_1-branch/libstdc++-v3/include/bits/basic_string.h