Bug 195970

Summary: C++ compile error with casted char pointer passed as reference
Product: Red Hat Enterprise Linux 4 Reporter: Hiroto Shibuya <shibuya>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 4.0CC: aoliva
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: 2006-12-13 10:44:45 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 Hiroto Shibuya 2006-06-20 01:13:58 UTC
Description of problem:

   Using gcc 3.4 on WS4, it generates error on C++ source which
   tries to cast unsigned char pointer to signed char pointer and
   then passes to a function as character pointer reference. 
   This code used to compile fine on RedHat Linux 8.0 with gcc 3.2.

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

    $ gcc --version
    gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)

How reproducible:

    100%

Steps to Reproduce:

1. Save the following source as C++ source, castandref.cc

------8<------ begin source -------8<-------
#include <stdio.h>

/*
This fails in WS4 with:

astandref.cc: In function `int main()':
castandref.cc:24: error: invalid initialization of non-const reference of type
'char*&' from a temporary of type 'char*'
castandref.cc:16: error: in passing argument 1 of `void x(char*&)'

due to the following bug

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16245
*/

void x(char *&s)
{
   s = "foobar";
}

main()
{
   unsigned char *s;

   x((char*)s);

   printf("%s\n", s);
}
------8<------ end source -------8<-------

2. Compile with gcc
  
Actual results:

$ gcc castandref.cc
castandref.cc: In function `int main()':
castandref.cc:24: error: invalid initialization of non-const reference of type
'char*&' from a temporary of type 'char*'
castandref.cc:16: error: in passing argument 1 of `void x(char*&)'

Expected results:

$ gcc castandref.cc
$ ./a.out
foobar

Additional info:

This seems to be a regression in gcc 3.4 described here.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16245

which claim to have it fixed in gcc 4.0 but not in 3.4.  
This works fine in gcc 3.3 and lower.

Comment 2 RHEL Program Management 2006-08-18 15:26:15 UTC
This request was evaluated by Red Hat Product Management for inclusion in a Red
Hat Enterprise Linux maintenance release.  Product Management has requested
further review of this request by Red Hat Engineering, for potential
inclusion in a Red Hat Enterprise Linux Update release for currently deployed
products.  This request is not yet committed for inclusion in an Update
release.

Comment 4 Alexandre Oliva 2006-12-08 17:47:36 UTC
GCC 3.2 was buggy in this regard.  A cast to a non-reference type yields an
rvalue, and binding rvalues to non-const references is forbidden.  The
referenced bug report is about an unrelated feature.