Search

Visual Studio 2010 SP1 Release x64 /O2 optimization bug (c2.dll version 10.00.40219.1 amd64) by btomko

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 688139
Opened: 9/13/2011 8:18:17 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
0
User(s) can reproduce this bug
Open a big endian binary file containing integers 15, 3, 6, 9, 12. Create a union to read 4 bytes at a time from the file. Create an integer called FIRST_INT_READ and set it equal to the first integer read from the bytestream (which is 15). Read the bytestream two more times in a loop into the union. Try now to use FIRST_INT_READ as the stopping criteria in a nested for loop. FIRST_INT_READ now has a value of the third position in the bytestream which is integer 6, unless you cout FIRST_INT_READ inside the for loop. Workarounds include:
1.) compiling in 32 bit mode
2.) turning off optimization in 64 bit
3.) declaring FIRST_INT_READ as volatile
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

Visual Studio 2010 SP1

Steps to reproduce

#include <iostream>
#include <fstream>

union ConversionUnion
{
    void Read(std::ifstream & fileStream) //big endian read 4 bytes
    {
        char ch;
        fileStream.get(ch);
        s[0] = ch;
        fileStream.get(ch);
        s[1] = ch;
        fileStream.get(ch);
        s[2] = ch;
        fileStream.get(ch);
        s[3] = ch;
    }
    
    char s[4];
    int i;
    float f;
};



int main()
{    
    //open big endian binary file containing integers 15, 3, 6, 9, 12
    std::ifstream fileStream("test.bin",std::fstream::in | std::fstream::binary);
    if(!fileStream.good())
    {
        std::cout << "error opening file\n";
        return 1;
    }

    ConversionUnion conv;        
    conv.Read(fileStream);
    int FIRST_INT_READ = conv.i;
    std::cout << "first int read: " << FIRST_INT_READ << "\n";    
    
    float myMin = 100;
    for(int x=0; x<2; ++x)
    {            
        conv.Read(fileStream);                    
        if(conv.f < myMin) myMin = conv.f;        
    }
    
    int *someIntArray = new int[1000];        
    for(int z=0; z<2; ++z)
    {        
        for(int y=0; y<FIRST_INT_READ; ++y)
        {            
            if (!someIntArray) return 1;
            int* destination = someIntArray+y/8;            
            //std::cout << "y: " << y << " first int read: " << FIRST_INT_READ << "\n" ;            
            std::cout << "y: " << y << "\n" ;
            destination[y+z] = z;            
        }
    }

    std::cout << "done\n";

    return 0;
}

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

first int read: 15
y: 0
y: 1
y: 2
y: 3
y: 4
y: 5
y: 0
y: 1
y: 2
y: 3
y: 4
y: 5
done

Expected results

first int read: 15
y: 0
y: 1
y: 2
y: 3
y: 4
y: 5
y: 6
y: 7
y: 8
y: 9
y: 10
y: 11
y: 12
y: 13
y: 14
y: 0
y: 1
y: 2
y: 3
y: 4
y: 5
y: 6
y: 7
y: 8
y: 9
y: 10
y: 11
y: 12
y: 13
y: 14
done
File Attachments
File Name Submitted By Submitted On File Size  
bug.cpp 9/13/2011 1 KB
test.bin 9/13/2011 20 bytes
Sign in to post a comment.
Posted by Microsoft on 9/30/2011 at 11:42 AM
This was an issue in the optimizer and has been fixed. The fix will appear in a subsequent release of the compiler. Thank you very much for reporting this issue and making our product better.

David Gillies
VC++ Optimizer Team
Posted by MS-Moderator09 [Feedback Moderator] on 9/13/2011 at 8:15 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 9/13/2011 at 8:43 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.