C#编译器中令人讨厌的错误,它会截断堆栈上的值 [英] Nasty bug in the C# compiler, it truncates values on the stack

查看:85
本文介绍了C#编译器中令人讨厌的错误,它会截断堆栈上的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法将其缩小到一个非常简单的表达方式。试试这个:


private void Bug()

{

bool b = false;

测试(3,(b || b)&& b&&!b);

}


private void Works()

{

bool b = true;

测试(3,(b || b)&& b&&!b );

}


private void Test(十进制v,bool b)

{

MessageBox.Show(v.ToString());

}


Test()中参数v的值为0!


如果更改函数Bug()并使bool b = true,则可以正常工作。我检查了MSIL代码,看起来编译器在

表达式上插入了(b || b)&& b&& !B"并默默地将之前的

参数截断为bool(1个字节)。由于参数是小数,它将显示零

,因为截断发生在十进制结构上。


您可以尝试使用任何结构和任何值作为第一个参数,

编译器将始终将堆栈上的结构截断为1个字节。如果你用自己的结构尝试

,你将丢失第一个字节后的所有数据。


奇怪的是,如果你删除括号b | | b&& b&& !B"是作品

罚款。


请不要专注于表达,这是一个真实的简化

表达。最糟糕的部分是没有编译器错误或警告,

它只是生成错误的MSIL代码。


这是一个已知错误吗?如果没有,我该如何向微软和

..NET团队报告?


我过去曾尝试过报告短路的错误运营商规格

和编译器但没有成功,缺陷仍然存在。


我也尝试使用最新的服务包,显示的版本.NET

配置模块版本为1.1.4322.573。

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.

推荐答案



"杰里" < JE ********** @ yandex.ru>在消息中写道

news:q68ee.10

"Jerry" <je**********@yandex.ru> wrote in message
news:q68ee.10


fQ2.2@trnddc05 ...
fQ2.2@trnddc05...
我设法将其缩小到一个非常简单的表达。试试这个:

private void Bug()
{
bool b = false;
测试(3,(b || b)&& b& ;&!b);
}

私人无效作品()
{
bool b = true;
考试(3,(b | | b)&& b&&!b);
}

私有空洞测试(十进制v,bool b)
{MessageBox。显示(v.ToString());
}

Test()中参数v的值为0!

如果更改函数Bug( )并使bool b = true它工作正常。我检查了MSIL代码,看起来编译器阻止了
表达式(b || b)&& b&& !B"并默默地将之前的
参数截断为bool(1个字节)。由于参数是小数,它将显示
零因为截断发生在十进制结构上。

你可以尝试使用任何结构和任何值作为第一个参数,
编译器总是将堆栈上的结构截断为1个字节。如果你用自己的结构尝试
,你将在第一个字节后丢失所有数据。

奇怪的是,如果删除括号b || b&& b&& !B"
工作
很好。

不要专注于表达,这是对真实表达的简化。最糟糕的部分是没有编译器错误或
警告,它只是生成错误的MSIL代码。

这是一个已知的错误吗?如果没有,我该如何向微软和.NET团队报告?

我过去曾尝试报告短路运营商规范和编译器的错误但是没有成功,缺陷仍然存在。

我也尝试使用最新的服务包,.NET
配置模块中显示的版本是1.1.4322.573版。
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.




是的,看起来像个bug。


你可以报告错误/问题
http://lab.msdn.microsoft.com/produc...k/ default.aspx


Willy。



Yep, looks like a bug.

You can report bugs/issues to
http://lab.msdn.microsoft.com/produc...k/default.aspx

Willy.


我可能会建议通过我们的内部升级到C#人员

路线..


-

鲍勃鲍威尔[MVP]

Visual C#, System.Drawing


在Windows窗体中查找优秀的Windows窗体文章提示和技巧
http://www.bobpowell.net/tipstricks.htm


答案使用GDI +常见问题解答的那些GDI +问题
http://www.bobpowell.net /faqmain.htm


所有新文章都提供C#和VB.NET中的代码。

订阅所提供的RSS源,绝不会错过任何新文章。


" Willy Denoyette [MVP]" <无线************* @ telenet.be>在消息中写道

news:%2 ****************** @ TK2MSFTNGP15.phx.gbl ...
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]" <wi*************@telenet.be> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...

杰瑞 < JE ********** @ yandex.ru>在消息中写道
新闻:q68ee.10

"Jerry" <je**********@yandex.ru> wrote in message
news:q68ee.10


这篇关于C#编译器中令人讨厌的错误,它会截断堆栈上的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆