Re: Nasty bug in the C# compiler, it truncates values on the stack

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Might I suggest that this gets escalated to the C# guys via our internal
route too..

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
news:%23%23aoKXNUFHA.3952@xxxxxxxxxxxxxxxxxxxxxxx
>
> "Jerry" <jerrygoodman@xxxxxxxxx> wrote in message
> news:q68ee.10$fQ2.2@xxxxxxxxxxx
>>I managed to narrow this down to a very simple expression. try this:
>>
>> private void Bug()
>> {
>> bool b = false;
>> Test(3, (b || b) && b && !b);
>> }
>>
>> private void Works()
>> {
>> bool b = true;
>> Test(3, (b || b) && b && !b);
>> }
>>
>> private void Test(decimal v, bool b)
>> {
>> MessageBox.Show(v.ToString());
>> }
>>
>> The value of the argument v in Test() is 0!
>>
>> If you change the function Bug() and make bool b = true it works fine. I
>> checked the MSIL code and it appears that the compiler chocked on the
>> expression "(b || b) && b && !b" and silently truncated the previous
>> argument to bool (1 byte). Since the argument is a decimal it will show
>> zero
>> because the truncation occurs on the decimal structure.
>>
>> You can try this with any struct and any value as the first argument, the
>> compiler will always truncate the struct on the stack to 1 byte. If you
>> try
>> with your own struct you will lose all the data after the first byte.
>>
>> Strangely enough, if you remove the parenthesis "b || b && b && !b" is
>> works
>> fine.
>>
>> Plese don't focus on the expression, it's a simplification of a real
>> expression. The nastiest part is that there is no compiler error or
>> warning,
>> it simply generates wrong MSIL code.
>>
>> Is this a known bug? If not, how do I report this to Microsoft and to the
>> .NET team?
>>
>> I tried in the past to report a bug with the shortcircuit operators specs
>> and compiler but didn't suceeded and the defect is still there.
>>
>> I also tried with the latest service pack, the version shown in the .NET
>> Configuration module is version 1.1.4322.573.
>>
>>
>
> Yep, looks like a bug.
>
> You can report bugs/issues to
> http://lab.msdn.microsoft.com/productfeedback/default.aspx
>
> Willy.
>
>


.



Relevant Pages

  • Strange problem with linked list code on windows OS
    ... I see strange problem while I run the below code on Windows using gcc ... under cygwin on windows and gcc compiler on any Linux system. ... struct list_head name = LIST_INIT ...
    (comp.lang.c)
  • Re: weird Windows 2000/XP bug
    ... Subject: weird Windows 2000/XP bug ... The problem is in the cmd.exe, it has the debug symbols ... I think compiler doesnot make much difference. ... Anything that support windows would be producing the same ...
    (Vuln-Dev)
  • Re: Common misconceptions about C (C95)
    ... p = (struct bar *) malloc; ... And in the former case I've "invited" the maintenance programmer to ... is the second version and not the first which the compiler will catch. ... in isolation regarding this particular bug. ...
    (comp.lang.c)
  • Re: Strange problem with linked list code on windows OS
    ... I see strange problem while I run the below code on Windows using gcc ... under cygwin on windows and gcc compiler on any Linux system. ... struct list_head name = LIST_INIT ...
    (comp.lang.c)
  • Re: copying c struct from arm to windows
    ... I copied the struct to a shared memory and then copyied it to ... in windows host code: ... The offset of struct A is 2 bytes diffrence from arm. ... [There are probably compiler options to make it do the slicing & dicing, but they might give you problems. ...
    (comp.sys.arm)