Bug 195970 - C++ compile error with casted char pointer passed as reference
C++ compile error with casted char pointer passed as reference
Status: CLOSED NOTABUG
Product: Red Hat Enterprise Linux 4
Classification: Red Hat
Component: gcc (Show other bugs)
4.0
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2006-06-19 21:13 EDT by Hiroto Shibuya
Modified: 2007-11-30 17:07 EST (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2006-12-13 05:44:45 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 Hiroto Shibuya 2006-06-19 21:13:58 EDT
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 Product and Program Management 2006-08-18 11:26:15 EDT
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 12:47:36 EST
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.

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