Bug 86523

Summary: g++ type casting bug for function arguments of reference type
Product: [Retired] Red Hat Linux Reporter: Wolfgang Reimer <linuxball>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: Brian Brock <bbrock>
Severity: high Docs Contact:
Priority: medium    
Version: 8.0CC: mitr
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2003-03-26 18:54:23 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Wolfgang Reimer 2003-03-25 01:30:13 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.2) Gecko/20030208
Netscape/7.02

Description of problem:
Type casting behavior ("unsigned char" to "char") for function / method
arguments of reference type depends on whether the program will be compiled
without or with optimization. Compiled without optimization it behaves as
expected and as gcc-2.96 behaved. Compiled with optimization (-0, -O1, -O2, or
-O3) it does not behave as expected.

See example bugtest.cc below:

// This demonstrates a behavior change (bug?) in gcc-3.2-7
// which occurs only when compiled with -O (-O1 ... -O3).
// The expected output "1 1" will occur only if compiled without optimization:
//   gcc -o bugtest bugtest.cc; ./bugtest
// Output will be "0 1" if compiled with gcc-3.2-7 and -O flag:
//   gcc -O -o bugtest bugtest.cc; ./bugtest

#include <iostream>

void proc(char & c) { c = 1; }

int main(void) {
        unsigned char uc1 = 0, uc2 = 0;

        // Works as expected (uc1 is set to 1) if compiled without -O
        // Behaves different (uc1's value remains 0) if compiled with -O:
        // Apparently a temporary variable is created and set instead of uc1.
        proc((char)uc1);

        // Work around: This works as expected with and without optimization
        proc((char&)uc2);

        std::cout << int(uc1) << " " << int(uc2) << "\n";
        return 0;
}


Version-Release number of selected component (if applicable):
gcc-3.2-7

How reproducible:
Always

Steps to Reproduce:
1. Copy,  paste, and save example (Description section) as bugtest.cc
2. Compile example using gcc-3.2-7:  g++ -O -o bugtest bugtest.cc
3. Execute binary: ./bugtest
    

Actual Results:  The output shown is "0 1".

Expected Results:  The output should be "1 1".

Additional info:

This did not happen with gcc-2.96 (RedHat 7.x) and it does not happen if
compiled using gcc-3.2-7 without optimization flag.

This gcc-3.2 behavior leads to crashes and unexpected behavior of a huge
multi-platform C++ project (Ptolemy) which I maintain for RedHat Linux (create
updated rpm packages).

A work around for gcc-3.2-7 is to use the reference type in casting (see example
code in section description). However, I did not check whether this works for
older gcc compilers, too.

Comment 1 Jakub Jelinek 2003-03-26 18:54:23 UTC
Your testcase is ill-formed. (char) c is an rvalue, and you can't bind a reference to non-const to an rvalue.