checking invariant using Microsoft code contracts

173 Views Asked by At

Just got exposed to the Microsoft Code contracts for checking pre-, post-condition and object invariants in the code (https://learn.microsoft.com/en-us/dotnet/framework/debug-trace-profile/code-contracts) and would like to try it out. One question I would like to confirm regarding soundness and completeness, given an invariant assuming the checker does not output any error messages, does it mean the invariant is indeed (provable) true or it still can be a false positive.

1

There are 1 best solutions below

0
Jeffrey L Whitledge On BEST ANSWER

The static checker can be fooled in various ways, such as adding a false assumption. I will assume in this answer that nothing like that has been done.

Also, it is possible that bugs exist in the checker. But assuming that there are none...

The static checker is designed not to produce false positives. All pre- and post-conditions and invariants will be checked, and they will only pass if the truth of the conditions can be positively verified. If a condition can not be verified, then an error message will be provided.

The system will not attempt to prove that an invariant can be violated. An "unproven" error message means that no proof of correctness has been found. The invariant may still be true, just unproven.

So there are no false positives (again, by design, assuming no bugs or sabotage).