Search

Possible optimization bug on manual bounds checking by 59C4DB0A-7734-4D58-A475-4F3AC3B7DCF8

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 626399
Opened: 12/1/2010 11:07:07 AM
Access Restriction: Public
0
Workaround(s)
1
User(s) can reproduce this bug
I found what appears to be an optimization bug in visual studio 2008 and 2010.
Details (expand)

Visual Studio/Silverlight/Tooling version

Visual Studio 2010

What category (if any) best represents this feedback?

Reliability

Steps to reproduce

Create a Win32 console project and copy the following code into the main .cpp file:

// bad_index_check.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void error()
{
    printf("whups!\n");
}

template <class t_type> inline void memory_to_string(const t_type &data)
{
    const size_t k_string_size= sizeof(t_type)*2 + 1;
    char blah[k_string_size];

    for (long byte_index= 0; byte_index<sizeof(data); ++byte_index)
    {
        long check_output_index= 2*byte_index + 1;
        if ((check_output_index)>=(0) && (check_output_index)<(k_string_size))
        {

        }
        else
        {
            error();
        }

        unsigned char value= reinterpret_cast<const unsigned char*>(&data)[byte_index];

        blah[byte_index*2]= value;
        blah[byte_index*2 + 1]= value;
    }

    return;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int blah;
    memory_to_string(blah);

    return 0;
}

Run the project in Release configuration

Product Language

English

Operating System

Windows 7

Operating System Language

English (US)

Actual results

The program prints:

whups!

Expected results

The program should print nothing. In the Debug configuration, it prints nothing.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 12/3/2010 at 10:49 AM
Thanks again for reporting this issue! We have a fix in the works which will be incorporated into a future release of Visual Studio.

In the meantime you can workaround this issue in your code by disabling optimizations in the function(s) that exhibits the incorrect behavior. This can be done via the optimize pragma:

#pragma optimize( "", off )
... // function exhibiting incorrect behavior
#pragma optimize( "", on )

More information on how to use this pragma can found here: http://msdn.microsoft.com/en-us/library/chh3fb0k(v=VS.100).aspx

Thanks again for helping us make continuing improvements to our compiler.

VC++ Codegen & Optimization Team
Posted by Microsoft on 12/1/2010 at 6:15 PM
Thanks for your feedback. We are routing 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 Microsoft on 12/1/2010 at 5:54 PM
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.