Home Dashboard Directory Help
Search

Bug in Visual Studio 2010 SP1 C++ compiler by Ben171280


Status: 

Closed
 as Fixed Help for as Fixed


2
0
Sign in
to vote
Type: Bug
ID: 700279
Opened: 11/10/2011 1:35:57 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

Please consider the following code:

int _tmain(int argc, _TCHAR* argv[])
{
    char c = 0xC3;
    char* pc = &c;
    unsigned short s = *pc;
    if(s > 0xFF)
        cout << "A" << endl;
    else
        cout << "B" << endl;
    return 0;
}

When compiled in VS 2010 SP1 in 32bit, this code does not produce the same result in debug and release mode: In debug "A" is displayed, in release "B" is displayed.

Now if I display s at the end of the function like this:

int _tmain(int argc, _TCHAR* argv[])
{
    char c = 0xC3;
    char* pc = &c;
    unsigned short s = *pc;
    if(s > 0xFF)
        cout << "A" << endl;
    else
        cout << "B" << endl;
    cout << s << endl;
    return 0;
}

The behaviour in release mode is changed and "A" is displayed.

It this a bug of the optimiser?

Ben
Details
Sign in to post a comment.
Posted by Microsoft on 11/11/2011 at 1:54 PM
Mike Danes is correct that the test case as is triggers warning C4309 and undefined behavior, however the undefined behavior is not causing the difference in generated code. If one replaces 0xC3 with the bit-wise equivalent -63, the incorrect program behavior persists.

As you surmised, the optimizer is incorrectly doing the conversion from char to unsigned short. I’ve marked this bug to be considered for our next compiler release.

ian Bearman
VC++ Code Generation and Optimization Team
Posted by MS-Moderator07 [Feedback Moderator] on 11/10/2011 at 6:40 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Posted by Mike Danes on 11/10/2011 at 10:43 AM
The C++ std says the following in [conv.integral]:

"If the destination type is signed, the value is unchanged if it can be represented in the destination type (and
bit-field width); otherwise, the value is implementation-defined."

Since char is signed and the integer constant 0xC3 cannot be represented in a char this means that your code triggers undefined behavior. The compiler actually emits a warning for this code: warning C4309: '=' : truncation of constant value.
Posted by MS-Moderator01 on 11/10/2011 at 1:48 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.