1

是否有 C# 工具可以静态(不执行代码)检测超出范围的数组访问,即会抛出IndexOutOfRangeException的数组访问。

谢谢你。

编辑:是的,我知道在一般情况下理论上是不可能做到的(即,它是不可判定的),但这并不意味着在某些情况下不可能做到(事实上整个领域形式验证是关于为理论上不可能的事情生产实用工具)。(我不认为这个表扬是特别需要的:))

4

3 回答 3

2

不,理论上是不可能的。这就是单元测试的目的;-)。

于 2013-12-06T16:46:12.190 回答
2

As Thomas and Heinzi have said, this is undecidable. There is a subset of your problem that is solvable - you could NGen (or JIT) your .NET application, and look for references to the IndexOutOfRangeException throw subroutine; the MSIL -> native compiler eliminates bounds checks (and thus IndexOutOfRangeExceptions) if it's absolutely certain that it simply can't occur.

In practice, that's usually code like for (int i = 0; i < ar.Length; i++) { ar[i] ... }, but it should trim down the undetermined cases considerably in many applications.

于 2013-12-06T16:52:43.467 回答
1

是的,.NET 的代码合同。请参阅MSDN 杂志中的这篇文章

于 2013-12-06T23:27:21.580 回答