J
Jan Bares
Hi,
I have found a bug in generated code in debug build. I have Visual C++ .NET
7.1.3088. Function Buggy::isBug() returns true and it should return false.
The sample is contrived to exhibit the bug in small code. The expression
m_array + sizeof(Buggy) * m_used
is actualy compiled as
m_array + sizeof(Buggy) * sizeof(Buggy) * m_used
If you look into assembly you should see something like this:
00411276 mov eax,dword ptr [this]
00411279 mov ecx,dword ptr [eax+1F8h]
0041127F imul ecx,ecx,1FCh
00411285 imul ecx,ecx,1FCh // <<<<<<<< ????
0041128B mov edx,dword ptr [this]
0041128E add ecx,dword ptr [edx+1F4h]
Thanks for any feedback, Jan
The sample code
===============
struct Buggy
{
char ch[500];// must be bigger otherwise shl will be used instead of imul
Buggy* m_array;
int m_used;
bool isBug()
{
m_array = 0;
m_used = 1;
return sizeof(Buggy) != int(m_array + sizeof(Buggy) * m_used);
}
};
int main()
{
Buggy test;
test.isBug();
return 0;
}
I have found a bug in generated code in debug build. I have Visual C++ .NET
7.1.3088. Function Buggy::isBug() returns true and it should return false.
The sample is contrived to exhibit the bug in small code. The expression
m_array + sizeof(Buggy) * m_used
is actualy compiled as
m_array + sizeof(Buggy) * sizeof(Buggy) * m_used
If you look into assembly you should see something like this:
00411276 mov eax,dword ptr [this]
00411279 mov ecx,dword ptr [eax+1F8h]
0041127F imul ecx,ecx,1FCh
00411285 imul ecx,ecx,1FCh // <<<<<<<< ????
0041128B mov edx,dword ptr [this]
0041128E add ecx,dword ptr [edx+1F4h]
Thanks for any feedback, Jan
The sample code
===============
struct Buggy
{
char ch[500];// must be bigger otherwise shl will be used instead of imul
Buggy* m_array;
int m_used;
bool isBug()
{
m_array = 0;
m_used = 1;
return sizeof(Buggy) != int(m_array + sizeof(Buggy) * m_used);
}
};
int main()
{
Buggy test;
test.isBug();
return 0;
}