Invalid type cast is compiled without error

Invalid type cast is compiled without error

Hello,

The Intel Compiler allows assigning an int* to a const int* &, which could cause unexpected behavior as in the following example:

int* p;
const int* & r = p;    // assign to reference to a pointer to const int - this must not be allowed
const int immutable = 0;
r = &immutable;    // assign p to address of immutable through r
*p = 1;    // whoops, now 0 = 1!

The code above compiles without errors,although the ASM generated ignores the assignment.

This potential issue may be hidden in template code, so it would be hard to detect without a compile error.

Regards,
Ricardo.

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Could someone comment on this issue? Thanks :)

Hi,
Thanks for reporting this issue. It should be a compiler bug. I have reported it to the compiler team for fix.
Thank you.
-Yang

Hello,this issue is fixed. I checked with 12.1 update 8, icl emits the following error msg now:

tt.cpp(8): error: a reference of type "const int *&" (not const-qualified) cannot be initialized with a value of type "int *"

Jennifer

Quoting Yang Wang (Intel)

Hi,
Thanks for reporting this issue. It should be a compiler bug. I have reported it to the compiler team for

[SergeyK] C++ language specificationshave tobe checked.

fix.
Thank you.
-Yang

Here are some results for different C/C++ compilers:

MS C/C++ with VS2005: Error C2440: 'initializing' : cannot convert from 'int *' to 'const int *&'
MinGW C/C++ v3.4.2 : Error: invalid initialization of reference of type 'const int*&' from expression of type 'int*'
Borland C/C++ v5.5.1 : Successfully compiled
Turbo C/C++ v3.0.0 : Successfully compiled

It looks like specifications have changed because two legacy C/C++ compilers compiled the code.

The subjectalso raises a veryold question: Could a value of some variable declared as 'const'be modified later?

Best regards,
Sergey

Quoting Yang Wang (Intel)

Hi,
Thanks for reporting this issue. It should be a compiler bug. I have reported it to the compiler team

[SergeyK]After a series of tests with different C/C++ compilersIconfirm it.

for fix.
Thank you.
-Yang

I simply would like toinform that a 3rd legacy compiler, that is Visual C++ v6.0 ( from Visual Studio 98 EE SP5 ) has the same problem.

Please take a look at a screenshot:

Quoting Sergey Kostrov...
The subjectalso raises a veryold question: Could a value of some variable declared as 'const'be
modified later?
...

Here is a Test-Case:

...
const int iN = 0;
printf( "Initial value for Variable of type < const int > is: %ld\n", iN );

for( int i = 0; i < 4; i++ )
{
printf( "New value for Variable of type < const int > is: %ld\n",
++( const_cast< int & >( iN ) ) );
}

printf( "Final value for Variable of type < const int > is: %ld\n", const_cast( int & )( iN ) );
printf( "Final value for Variable of type < int > is: %ld\n", iN );
...

Output:

Initial value for Variable of type < const int > is: 0

New value for Variable of type < const int >is: 1
New value for Variable of type < const int >is: 2
New value for Variable of type < const int >is: 3
New value for Variable of type < const int >is: 4

Final value for Variable of type < const int > is: 4
Final value for Variable of type < int > is: 0

Quoting Sergey KostrovThe subjectalso raises a veryold question: Could a value of some variable declared as 'const'be modified later?

Best regards,
Sergey

The constness is supposed to be a compile-time helper against programming errors, but the programmer still has the ultimate decision about how the variable shall be used.When const_cast is used, the programmer is explicitly telling the compiler "I know what I'm doing, don't complain", so it's allowed to modify a const variable in that case.

However the error that I reported initially is not like an explicit const cast, instead it may be hidden by template transformations that end up generating incorrect/invalid code against the programmer's will, thus the importance of detecting that situation with a compile error.

Leave a Comment

Please sign in to add a comment. Not a member? Join today