【发布时间】:2011-01-09 06:45:04
【问题描述】:
我在代码中遇到了一个错误,只有在启用优化的情况下构建代码时才会重现该错误。我制作了一个控制台应用程序,它复制了测试逻辑(下面的代码)。您会看到,启用优化后,执行此无效逻辑后,“值”变为空:
if ((value == null || value == new string[0]) == false)
修复很简单,并在有问题的代码下方注释掉。但是...我更担心我可能遇到了汇编程序中的错误,或者其他人可能解释了为什么 value 设置为 null。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace memory_testing
{
class Program
{
sta tic void Main(string[] args)
{
while(true)
{
Console.Write("Press any key to start...");
Console.ReadKey();
Console.WriteLine();
PrintManagerUser c = new PrintManagerUser();
c.MyProperty = new string[1];
}
}
}
public class PrintManager
{
public void Print(string key, object value)
{
Console.WriteLine("Key is: " + key);
Console.WriteLine("Value is: " + value);
}
}
public class PrintManagerUser
{
public string[] MyProperty
{
get { return new string[100]; }
set
{
Console.WriteLine("Pre-check Value is: " + value);
if ((value == null || value == new string[0]) == false)
{
Console.WriteLine("Post-check Value is: " + value);
new PrintManager().Print("blah", value);
}
//if (value != null && value.Length > 0)
//{
// new PrintManager().Print("blah", value);
//}
}
}
}
}
正常的输出应该是:
Pre-check Value is: System.String[]
Post-check Value is: System.String[]
Key is: blah
Value is: System.String[]
错误输出是:
Pre-check Value is: System.String[]
Post-check Value is:
Key is: blah
Value is:
My Env 是一个运行 Windows Server 2003 R2 和 .NET 3.5 SP1 的 VM。使用VS2008团队系统。
谢谢,
布赖恩
【问题讨论】:
-
显然,优化器对你使用 '== false' 而不是应用 !表达式的运算符。
-
当你需要 Eric Lippert 时他在哪里 ;-p
-
我会把这个转发给 jitter 团队。谢谢!
-
看,人们 - @Eric 是 Beetlejuice,说出他的时间 3 次,他就会出现。
-
这已被分配 CVE-2011-1271 - Microsoft .NET Framework 4 beta 2 之前的 JIT 编译器,当 IsJITOptimizerDisabled 为 false 时,不能正确处理与空字符串相关的表达式,这允许上下文-依赖攻击者通过利用精心设计的应用程序绕过机会主义情况下的预期访问限制,如 x86 平台上的 C# 应用程序所示。