문제

Is there a tool for C# which can statically (without executing the code) detect out of bound array access, i.e., array access that will throw IndexOutOfRangeException.

Thank you.

EDIT: Yes, I am aware that it is a theoretically impossible to do it in general case (i.e., it is undecidable), but that that does not mean that it is not possible to do it for some cases (in fact the whole field of formal verification is about producing practical tools for theoretically impossible things). (I didn't think this commend was specially required :))

도움이 되었습니까?

다른 팁

No, it's theoretically just not possible. This is what unit tests are made for ;-).

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top