Bug 86523 - g++ type casting bug for function arguments of reference type
g++ type casting bug for function arguments of reference type
Status: CLOSED NOTABUG
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
8.0
i686 Linux
medium Severity high
: ---
: ---
Assigned To: Jakub Jelinek
Brian Brock
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2003-03-24 20:30 EST by Wolfgang Reimer
Modified: 2007-04-18 12:52 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2003-03-26 13:54:23 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Wolfgang Reimer 2003-03-24 20:30:13 EST
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 13:54:23 EST
Your testcase is ill-formed. (char) c is an rvalue, and you can't bind a reference to non-const to an rvalue.

Note You need to log in before you can comment on or make changes to this bug.