A C++ implementation that detects undefined behavior?

后端 未结 10 1339
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 14:13

A huge number of operations in C++ result in undefined behavior, where the spec is completely mute about what the program\'s behavior ought to be and allows for anything to

相关标签:
10条回答
  • 2020-11-27 14:51

    Take a look at PCLint its pretty decent at detecting a lot of bad things in C++.

    Here's a subset of what it catches

    0 讨论(0)
  • 2020-11-27 14:52

    Undefined behaviour is undefined. The best you can do is conform to the standard pedantically, as others have suggested, however, you can not test for what is undefined, because you don't know what it is. If you knew what it was and standards specified it, it would not be undefined.

    However, if you for some reason, do actually rely on what the standard says is undefined, and it results in a particular result, then you may choose to define it, and write some unit tests to confirm that for your particular build, it is defined. It is much better, however, to simply avoid undefined behaviour whenever possible.

    0 讨论(0)
  • 2020-11-27 14:55

    Just as a side observation, according to the theory of computability, you cannot have a program that detects all possible undefined behaviours.

    You can only have tools that use heuristics and detect some particular cases that follow certain patterns. Or you can in certain cases prove that a program behaves as you want. But you cannot detect undefined behaviour in general.

    Edit

    If a program does not terminate (hangs, loops forever) on a given input, then its output is undefined.

    If you agree on this definition, then determining whether a program terminates is the well-known "Halting Problem", which has been proven to be undecidable, i.e. there exists no program (Turing Machine, C program, C++ program, Pascal program, in whatever language) that can solve this problem in general.

    Simply put: there exists no program P that can take as input any program Q and input data I and print as output TRUE if Q(I) terminates, or else print FALSE if Q(I) does not terminate.

    For more information you can look at http://en.wikipedia.org/wiki/Halting_problem.

    0 讨论(0)
  • 2020-11-27 14:59

    Clang has a suite of sanitizers that catch various forms of undefined behavior. Their eventual goal is to be able to catch all C++ core language undefined behavior, but checks for a few tricky forms of undefined behavior are missing right now.

    For a decent set of sanitizers, try:

    clang++ -fsanitize=undefined,address
    

    -fsanitize=address checks for use of bad pointers (not pointing to valid memory), and -fsanitize=undefined enables a set of lightweight UB checks (integer overflow, bad shifts, misaligned pointers, ...).

    -fsanitize=memory (for detecting uninitialized memory reads) and -fsanitize=thread (for detecting data races) are also useful, but neither of these can be combined with -fsanitize=address nor with each other because all three have an invasive impact on the program's address space.

    0 讨论(0)
提交回复
热议问题