Skip to content

Instantly share code, notes, and snippets.

@daverigby
Last active March 15, 2018 14:18
Show Gist options
  • Save daverigby/979ef668ab89c2b6302f805cf6c96dc1 to your computer and use it in GitHub Desktop.
Save daverigby/979ef668ab89c2b6302f805cf6c96dc1 to your computer and use it in GitHub Desktop.
Example of UBSan enum error
$ 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