Loophole in Visual C++, Part 2

Here is a slightly more elaborate example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <limits.h>
 
unsigned int ratio(unsigned int x, unsigned int y)
{
    if (x <= UINT_MAX / 100) x *= 100; else y /= 100;
 
    if (y == 0) y = 1;
 
    return x / y;
}
 
int main(void)
{
    unsigned int count;
 
    for (count = 0x3fffffff; count != 0; ++count)
    {
        /* do something */
 
        /* show progress */
        printf("\r%u%% done", ratio(count, UINT_MAX));
    }
 
    return 0;
}

This program goes through the entire range of the unsigned int type, performing some action for each. It shows the progress by calling a function to compute the ratio of count to the maximum possible value. Again, count is incremented in each step, and hence will reach the value zero at some point.

The program works as expected on the compilers I tried, except for cl.exe from VC7 and VC71 with the /O2 switch, which stop at 25%. In case you wondered about the starting point of 0x3fffffff, that’s the reason — no need to watch your machine chew it’s way through all integers up to 25%.

Looking at the code generated for the loop:

1
2
3
4
5
$L873:
        ...
        inc    esi
        add    edi, 100                ; 00000064H
        jne    short $L873

We see that it fails because the two instructions before the conditional jump have been reversed. Again it looks like the optimizer fails to recognize the importance of the increment to the loop.

pixelstats trackingpixel
Share

4 thoughts on “Loophole in Visual C++, Part 2

  1. Believe it or not this bug also exist on the gcc/g++ compiler. Even without optimizations (I don’t know if you can optimize code on gcc? Can you?)

  2. It is still present in Visual C++ 2008 SP1, and from the reply on the Visual C++ forum it appears to also be in the VS 2010 beta.

    I have submitted a bug report to Microsoft.

  3. Microsoft responded to the bug report:

    “Thanks again for reporting this issue. It has been addressed and a fix will be available in the next major release of the C++ compiler.”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">