Last active
March 15, 2018 14:18
-
-
Save daverigby/979ef668ab89c2b6302f805cf6c96dc1 to your computer and use it in GitHub Desktop.
Example of UBSan enum error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ cat undefined.cc | |
| #include <stdexcept> | |
| enum Enum { | |
| A, | |
| B, | |
| C, | |
| Invalid | |
| }; | |
| bool decode(int userInput, Enum& e) { | |
| e = static_cast<Enum>(userInput); | |
| return e < Invalid; | |
| } | |
| int main() { | |
| Enum e; | |
| decode(2, e); // true | |
| decode(10, e); // false | |
| // do something with e... | |
| } | |
| $ clang++ undefined.cc -fsanitize=undefined | |
| $ ./a.out | |
| undefined.cc:12:12: runtime error: load of value 10, which is not a valid value for type 'Enum' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment