Consider the following code.
//We use c++ version 98.
#include <iostream>
using namespace std;
//declaration of this enum type is in C header file.
typedef enum Color {
Red=1,
Blue=11
} Color;
//Signature of this function can't be changed
int func(Color c){
//Following line causes UndefinedBehaviour in clang++ if
//value is not a valid Color value
return (int)c;
}
int main(){
int x;
cin >> x;
cout << func((Color)x) << endl;
return 0;
}
//Compilation:
//clang++ -fsanitize=undefined main.cpp
Now we want to check if value of c
in func
is valid and also want to avoid "Undefined behaviour". But we do not have any control over the caller of the func
.
Note that the declaration of Color
is in C
header file.