Purging Undefined Behavior & Intel Assumptions in a Legacy C++ Codebase - Roth Michaels CppCon 2022

Learn how to purge undefined behavior and Intel assumptions from a legacy C++ codebase, improving reliability, maintainability, and performance, while avoiding the fear of UB through tools and cultural changes.

Key takeaways
  • Understanding undefined behavior: Undefined behavior (UB) is a common problem in C++ codebases, where the behavior of a program is not defined by the standard. UB can lead to unexpected behavior, crashes, or incorrect results.
  • Consequences of UB: UB can cause bugs, crashes, or incorrect results in unexpected places, making it hard to debug. It can also cause non-deterministic behavior, leading to inconsistencies across different platforms or environments.
  • Fear of UB: Fear of UB can lead to defensive programming, where developers add workarounds to avoid UB, making the codebase more complex and harder to maintain.
  • Tools for UB detection: Various tools can be used to detect UB, including Clang static analyzers, runtime analyzers, and sanitizers (e.g., ThreadSanitizer, MemorySanitizer, and UndefinedBehaviorSanitizer).
  • Implementing tooling changes: Implementing tooling changes can help detect and fix UB in a codebase. This includes writing Clang refactoring tools, implementing automatic formatting checks, and using Clang static analyzers.
  • Educating developers: Educating developers about UB is crucial to ensure they understand the implications of UB and how to avoid it.
  • Cultural changes: Cultural changes, such as treating UB as a critical issue and prioritizing fixes, can help drive a culture of quality and maintainability.
  • Fixing UB: Fixing UB requires understanding the problem, identifying the cause, and addressing it. This may involve rewriting code, adding checks, or using alternative libraries.
  • Portability: Considerations for portability are essential when fixing UB, as UB can be platform-dependent.