Undefined Behavior is Not an Error
summary
Built a C++ cross‑platform library suite (CopperSpice) and a forked documentation tool (doxypress), then hit real undefined behavior: a non‑null‑terminated QString lets clang optimize away loop code, static initialization order breaks startup, and sequencing/argument evaluation rules change behavior between C++11 and C++17. Uses concrete fixes (make string APIs safer, wrap statics in function locals, rewrite metadata), and relies on clang sanitizers and cross‑compiler/optimization testing to find UB. Treat UB as a P0 bug.
AI-written summary · about
chapters (9)
- 0:00Start
- 3:01what does undefined behavior represent for a compiler developer ■fascinating and intriguing theoretical discussion ■possible masters degree subject for a compiler designer ■influences the way they write an optimizer
- 3:47what do C++ programmers believe about undefined behavior ■compilers should report undefined behavior as an error experienced developers can avoid bad code ■not my responsibility ■undefined behavior is easy to spot and simple to debug
- 19:03access an element of a container which is out of bounds 。 operator[] returns a reference to an element in the string no test to verify index + 1 and index + 2 are valid positions when the loop reaches the end of the string undefined behavior
- 22:46o relational comparison of pointers is only defined if the pointers point to members of the same object or elements of the same array ifs is replaced with the standard says you must return false
- 25:12modifying an object which was originally declared const keyword const_cast ■used to remove the "constness" so the data can be modified ■using this approach often means there is a design flaw modifying tmp is undefined behavior if the passed argument was originally declared const
- 29:14actions in C++ which produce side effects ■reading a volatile object write access to any object ■calling a library function which performs 1/0 ■Invoking a function which does any one of the above
- 33:55order of evaluation for passed parameters to a function originally unspecified, execution could be interleaved C++17 changed this to to indeterminately sequenced ⚫ evaluation of arguments is "sequenced before" the function call
- 42:28similar examples are shown as being defined in C++17 which exhibit the same compiler warning messages
