Note: This is a beta release of Red Hat Bugzilla 5.0. The data contained within is a snapshot of the live data so any changes you make will not be reflected in the production Bugzilla. Also email is disabled so feel free to test any aspect of the site that you want. File any problems you find or give feedback here.
Bug 86523 - g++ type casting bug for function arguments of reference type
Summary: g++ type casting bug for function arguments of reference type
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 8.0
Hardware: i686
OS: Linux
medium
high
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Brian Brock
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2003-03-25 01:30 UTC by Wolfgang Reimer
Modified: 2007-04-18 16:52 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2003-03-26 18:54:23 UTC


Attachments (Terms of Use)

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.


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