473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Visual C++ optimizer bug (reported by a friend in the Computer Chess Club)

When optimization is turned on (these settings will reproduce it along with others):
/Ox /Og /Ob2 /Oi /Ot /Oy /GT /GL /G6 /GA /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /GF /FD /EHsc /ML /Gy /Fp".\Release/bug.pch" /FAcs /Fa".\Release/" /Fo".\Release/" /Fd".\Release/" /W4 /nologo /c /TP

This program crashes heinously:

#include <stdio.h>
#include <stdlib.h>
unsigned int a[144];

void init ()
{
unsigned int i;
for (i = 0; i < 144; i++)
a[i] = rand();
}

void dump ()
{
unsigned int row, col;
for (row = 11; row < 12; row--) // <--- PROBLEM: row < 12
{
for (col = 0; col < 12; col++)
printf("%u ", a[(row * 12) + col]);
putchar('\n');
}
}

int main ()
{
init();
dump();
return 0;
}

Here is the resultant assembly language:

; Listing generated by Microsoft (R) Optimizing Compiler Version 13.10.3077

TITLE .\bug.cpp
.386P
include listing.inc
if @Version gt 510
..model FLAT
else
; COMDAT ??_C@_03IOOJPBPG@?$CFu?5?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT ?init@@YAXXZ
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT ?dump@@YAXXZ
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif

INCLUDELIB LIBC
INCLUDELIB OLDNAMES

PUBLIC ??_C@_03IOOJPBPG@?$CFu?5?$AA@ ; `string'
PUBLIC ?a@@3PAIA ; a
EXTRN __iob:BYTE
EXTRN __flsbuf:NEAR
EXTRN _rand:NEAR
EXTRN _printf:NEAR
?a@@3PAIA DD 090H DUP (?) ; a
; COMDAT ??_C@_03IOOJPBPG@?$CFu?5?$AA@
CONST SEGMENT
??_C@_03IOOJPBPG@?$CFu?5?$AA@ DB '%u ', 00H ; `string'
CONST ENDS
PUBLIC ?dump@@YAXXZ ; dump
; Function compile flags: /Ogty
; File c:\tmp\bug.cpp
; COMDAT ?dump@@YAXXZ
_TEXT SEGMENT
?dump@@YAXXZ PROC NEAR ; dump, COMDAT

; 13 : {

00000 53 push ebx
00001 56 push esi
00002 57 push edi

; 14 : unsigned int row, col;
; 15 : for (row = 11; row < 12; row--) // <--- PROBLEM: row < 12

00003 bb 10 02 00 00 mov ebx, OFFSET FLAT:?a@@3PAIA+528
00008 eb 06 8d 9b 00
00 00 00 npad 8
$L980:

; 16 : {
; 17 : for (col = 0; col < 12; col++)

00010 8b f3 mov esi, ebx
00012 bf 0c 00 00 00 mov edi, 12 ; 0000000cH
$L983:

; 18 : printf("%u ", a[(row * 12) + col]);

00017 8b 06 mov eax, DWORD PTR [esi]
00019 50 push eax
0001a 68 00 00 00 00 push OFFSET FLAT:??_C@_03IOOJPBPG@?$CFu?5?$AA@
0001f e8 00 00 00 00 call _printf
00024 83 c4 08 add esp, 8
00027 83 c6 04 add esi, 4
0002a 4f dec edi
0002b 75 ea jne SHORT $L983

; 19 : putchar('\n');

0002d ff 0d 24 00 00
00 dec DWORD PTR __iob+36
00033 78 11 js SHORT $L1000
00035 8b 0d 20 00 00
00 mov ecx, DWORD PTR __iob+32
0003b c6 01 0a mov BYTE PTR [ecx], 10 ; 0000000aH
0003e ff 05 20 00 00
00 inc DWORD PTR __iob+32
00044 eb 0f jmp SHORT $L981
$L1000:
00046 68 20 00 00 00 push OFFSET FLAT:__iob+32
0004b 6a 0a push 10 ; 0000000aH
0004d e8 00 00 00 00 call __flsbuf
00052 83 c4 08 add esp, 8
$L981:
00055 83 eb 30 sub ebx, 48 ; 00000030H
00058 81 fb 40 02 00
00 cmp ebx, OFFSET FLAT:?a@@3PAIA+576
0005e 72 b0 jb SHORT $L980
00060 5f pop edi
00061 5e pop esi
00062 5b pop ebx

; 20 : }
; 21 : }

00063 c3 ret 0
?dump@@YAXXZ ENDP ; dump
_TEXT ENDS
PUBLIC ?init@@YAXXZ ; init
; Function compile flags: /Ogty
; COMDAT ?init@@YAXXZ
_TEXT SEGMENT
?init@@YAXXZ PROC NEAR ; init, COMDAT

; 6 : {

00000 56 push esi

; 7 : unsigned int i;
; 8 : for (i = 0; i < 144; i++)

00001 33 f6 xor esi, esi
$L973:

; 9 : a[i] = rand();

00003 e8 00 00 00 00 call _rand
00008 89 86 00 00 00
00 mov DWORD PTR ?a@@3PAIA[esi], eax
0000e 83 c6 04 add esi, 4
00011 81 fe 40 02 00
00 cmp esi, 576 ; 00000240H
00017 72 ea jb SHORT $L973
00019 5e pop esi

; 10 : }

0001a c3 ret 0
?init@@YAXXZ ENDP ; init
_TEXT ENDS
PUBLIC _main
; Function compile flags: /Ogty
; COMDAT _main
_TEXT SEGMENT
_main PROC NEAR ; COMDAT

; 24 : {

00000 56 push esi

; 25 : init();

00001 33 f6 xor esi, esi
$L1017:
00003 e8 00 00 00 00 call _rand
00008 89 86 00 00 00
00 mov DWORD PTR ?a@@3PAIA[esi], eax
0000e 83 c6 04 add esi, 4
00011 81 fe 40 02 00
00 cmp esi, 576 ; 00000240H
00017 72 ea jb SHORT $L1017

; 26 : dump();

00019 e8 00 00 00 00 call ?dump@@YAXXZ ; dump

; 27 : return 0;

0001e 33 c0 xor eax, eax
00020 5e pop esi

; 28 : }

00021 c3 ret 0
_main ENDP
_TEXT ENDS
END

Nov 17 '05 #1
2 1953
Truly awful code, but AFICT leagal according to the C++ standard. Sure
looks like a bug to me too. I'll make sure MS knows about it if it's not
already fixed in Whidbey.
-cd

Dann Corbit wrote:
When optimization is turned on (these settings will reproduce it
along with others): /Ox /Og /Ob2 /Oi /Ot /Oy /GT /GL /G6 /GA /D
"WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /GF /FD /EHsc /ML /Gy
/Fp".\Release/bug.pch" /FAcs /Fa".\Release/" /Fo".\Release/"
/Fd".\Release/" /W4 /nologo /c /TP

This program crashes heinously:

#include <stdio.h>
#include <stdlib.h>
unsigned int a[144];

void init ()
{
unsigned int i;
for (i = 0; i < 144; i++)
a[i] = rand();
}

void dump ()
{
unsigned int row, col;
for (row = 11; row < 12; row--) // <--- PROBLEM: row < 12
{
for (col = 0; col < 12; col++)
printf("%u ", a[(row * 12) + col]);
putchar('\n');
}
}

int main ()
{
init();
dump();
return 0;
}

Here is the resultant assembly language:

; Listing generated by Microsoft (R) Optimizing Compiler Version
13.10.3077

TITLE .\bug.cpp
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
; COMDAT ??_C@_03IOOJPBPG@?$CFu?5?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT ?init@@YAXXZ
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT ?dump@@YAXXZ
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif

INCLUDELIB LIBC
INCLUDELIB OLDNAMES

PUBLIC ??_C@_03IOOJPBPG@?$CFu?5?$AA@ ; `string'
PUBLIC ?a@@3PAIA ; a
EXTRN __iob:BYTE
EXTRN __flsbuf:NEAR
EXTRN _rand:NEAR
EXTRN _printf:NEAR
?a@@3PAIA DD 090H DUP (?) ; a
; COMDAT ??_C@_03IOOJPBPG@?$CFu?5?$AA@
CONST SEGMENT
??_C@_03IOOJPBPG@?$CFu?5?$AA@ DB '%u ', 00H ; `string'
CONST ENDS
PUBLIC ?dump@@YAXXZ ; dump
; Function compile flags: /Ogty
; File c:\tmp\bug.cpp
; COMDAT ?dump@@YAXXZ
_TEXT SEGMENT
?dump@@YAXXZ PROC NEAR ; dump, COMDAT

; 13 : {

00000 53 push ebx
00001 56 push esi
00002 57 push edi

; 14 : unsigned int row, col;
; 15 : for (row = 11; row < 12; row--) // <--- PROBLEM: row
< 12

00003 bb 10 02 00 00 mov ebx, OFFSET FLAT:?a@@3PAIA+528
00008 eb 06 8d 9b 00
00 00 00 npad 8
$L980:

; 16 : {
; 17 : for (col = 0; col < 12; col++)

00010 8b f3 mov esi, ebx
00012 bf 0c 00 00 00 mov edi, 12 ; 0000000cH
$L983:

; 18 : printf("%u ", a[(row * 12) + col]);

00017 8b 06 mov eax, DWORD PTR [esi]
00019 50 push eax
0001a 68 00 00 00 00 push OFFSET FLAT:??_C@_03IOOJPBPG@?$CFu?5?$AA@
0001f e8 00 00 00 00 call _printf
00024 83 c4 08 add esp, 8
00027 83 c6 04 add esi, 4
0002a 4f dec edi
0002b 75 ea jne SHORT $L983

; 19 : putchar('\n');

0002d ff 0d 24 00 00
00 dec DWORD PTR __iob+36
00033 78 11 js SHORT $L1000
00035 8b 0d 20 00 00
00 mov ecx, DWORD PTR __iob+32
0003b c6 01 0a mov BYTE PTR [ecx], 10 ; 0000000aH
0003e ff 05 20 00 00
00 inc DWORD PTR __iob+32
00044 eb 0f jmp SHORT $L981
$L1000:
00046 68 20 00 00 00 push OFFSET FLAT:__iob+32
0004b 6a 0a push 10 ; 0000000aH
0004d e8 00 00 00 00 call __flsbuf
00052 83 c4 08 add esp, 8
$L981:
00055 83 eb 30 sub ebx, 48 ; 00000030H
00058 81 fb 40 02 00
00 cmp ebx, OFFSET FLAT:?a@@3PAIA+576
0005e 72 b0 jb SHORT $L980
00060 5f pop edi
00061 5e pop esi
00062 5b pop ebx

; 20 : }
; 21 : }

00063 c3 ret 0
?dump@@YAXXZ ENDP ; dump
_TEXT ENDS
PUBLIC ?init@@YAXXZ ; init
; Function compile flags: /Ogty
; COMDAT ?init@@YAXXZ
_TEXT SEGMENT
?init@@YAXXZ PROC NEAR ; init, COMDAT

; 6 : {

00000 56 push esi

; 7 : unsigned int i;
; 8 : for (i = 0; i < 144; i++)

00001 33 f6 xor esi, esi
$L973:

; 9 : a[i] = rand();

00003 e8 00 00 00 00 call _rand
00008 89 86 00 00 00
00 mov DWORD PTR ?a@@3PAIA[esi], eax
0000e 83 c6 04 add esi, 4
00011 81 fe 40 02 00
00 cmp esi, 576 ; 00000240H
00017 72 ea jb SHORT $L973
00019 5e pop esi

; 10 : }

0001a c3 ret 0
?init@@YAXXZ ENDP ; init
_TEXT ENDS
PUBLIC _main
; Function compile flags: /Ogty
; COMDAT _main
_TEXT SEGMENT
_main PROC NEAR ; COMDAT

; 24 : {

00000 56 push esi

; 25 : init();

00001 33 f6 xor esi, esi
$L1017:
00003 e8 00 00 00 00 call _rand
00008 89 86 00 00 00
00 mov DWORD PTR ?a@@3PAIA[esi], eax
0000e 83 c6 04 add esi, 4
00011 81 fe 40 02 00
00 cmp esi, 576 ; 00000240H
00017 72 ea jb SHORT $L1017

; 26 : dump();

00019 e8 00 00 00 00 call ?dump@@YAXXZ ; dump

; 27 : return 0;

0001e 33 c0 xor eax, eax
00020 5e pop esi

; 28 : }

00021 c3 ret 0
_main ENDP
_TEXT ENDS
END

Nov 17 '05 #2
Carl Daniel [VC++ MVP] wrote:
Truly awful code, but AFICT leagal according to the C++ standard.
Sure looks like a bug to me too. I'll make sure MS knows about it if
it's not already fixed in Whidbey.


This is fixed in Whidbey.

-cd
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will...
14
by: Bupp Phillips | last post by:
I have a customer table that has the field CUSTOMER_ID as the primary key (cust_pkkey), the table has 102,834 records in it. The following select statement works fine: select * from customer...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
26
by: Bruno Jouhier [MVP] | last post by:
I'm currently experiencing a strange phenomenon: At my Office, Visual Studio takes a very long time to compile our solution (more than 1 minute for the first project). At home, Visual Studio...
97
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in...
63
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html...
1
by: softwaregeek | last post by:
hi all, i have given a tutorial to develop chess game (one player). I want to know from you that how should i start.which are the things that need to be considered.I have searched on net and got...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.