But just to be clear @chongli is logical
Think of UB as a probabilistic error. I.e. it is always stupid to rely on it
1. Write code without errors -- sensible
2. Allow compilers to assume the absence of errors -- occasionally sensible, since it speeds up your program
In defence of UB, for the most part they are things that should break your program anyway: stack overflow is never correct. So your choice is mostly to fail badly quickly, or to fail slowly well
Thanks to google making the UB sanitizers you are free to make that choice even in C