[Bug c++/51270] New: constness violation is accepted without any warning but leads to a required function call being eliminated during optimization

2011-11-22 Thread michiel_dewilde at agilent dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51270

 Bug #: 51270
   Summary: constness violation is accepted without any warning
but leads to a required function call being eliminated
during optimization
Classification: Unclassified
   Product: gcc
   Version: 4.6.2
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: michiel_dewi...@agilent.com


Created attachment 25884
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25884
Standalone source file

Bug summary: constness violation is accepted without any warning but leads to a
required function call being eliminated during optimization

The code itself has a problem in the function "faulty_compiled_function":
Feeding the return value of "pass_through" as argument to "recast_reference"
violates "const" regulations.
This error is never caught by at least g++ 4.4.2 and g++ 4.6.2 on x86_64.
Far worse, when compiling with -O1 or bigger, the optimizer eliminates the call
to "pass_through" and feeds invalid data into "recast_reference".

Proposed treatment: Either there must be an error keeping this code from
compiling, or the optimization must be able to handle it properly.

Bug triggered as follows:
  Compile using "g++ -O faulty_optimization_of_invalid_const_usage.cpp"
  Run "./a.out ; echo $0"
This should return exit code 0. It returns exit code 1.

Without optimization, there is no error.
The error occurs at any nonzero optimization level, even when using
-fno-strict-aliasing -fno-inline -fno-omit-stack-frame.

$ gcc -v
COLLECT_GCC=gcc_x86_64
COLLECT_LTO_WRAPPER=/gfs/belgium/gntnas01/d3/hped_build_gent/tools/gcc/4.6.2/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --prefix=/hped/builds/tools/gcc/4.6.2
--enable-languages=c,c++ --with-gmp=/hfs/d1/local/dbjornba/btmp
--with-mpfr=/hfs/d1/local/dbjornba/btmp --with-mpc=/hfs/d1/local/dbjornba/btmp
Thread model: posix
gcc version 4.6.2 (GCC)


[Bug c++/51270] constness violation is accepted without any warning but leads to a required function call being eliminated during optimization

2011-11-22 Thread michiel_dewilde at agilent dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51270

--- Comment #1 from Michiel De Wilde  
2011-11-22 15:26:42 UTC ---
Created attachment 25885
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25885
preprocessed source code (*.ii)


[Bug c++/51270] missed warning about returning reference to temporary

2011-11-22 Thread michiel_dewilde at agilent dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51270

--- Comment #6 from Michiel De Wilde  
2011-11-22 18:20:44 UTC ---
Hi Jonathan, thanks for the quick analysis.

I did not realize that "char * &" and "char const * &" are not
reference-compatible, leading to a temporary being introduced when initializing
a "char const * const &" reference from a "char * &".

Therefore this is not a true gcc bug.

It would be nice though to have a warning for this case, i.e. when only the
different constnesses after pointer dereferencing are responsible for
temporaries being created for const lvalue initializations from other lvalues.
I understand this may be difficult.
Improved tracking of the reference to the temporary would also be nice.

Thanks,

Michiel