Undefined Behavior
$1.00
Minimum price
$9.99
Suggested price

Undefined Behavior

A Guide to Programming's Dark Corner

About the Book

Life would be much simpler if computers just did exactly what we told them to do. Unfortunately, the reality is far more complex. In the realm of software development, we frequently encounter situations where our assumptions about program behavior clash with the actual mechanics of how computers execute our code. This disconnect is perhaps most dramatically illustrated in the concept of undefined behavior.
  • Share this book

  • Categories

    • Computers and Programming
    • Software Engineering
  • Feedback

    Email the Author(s)

About the Author

Thomas Peuß
Thomas Peuß

The author holds a Master's degree in Computer Science and brings decades of hands-on experience to their work. Their career spans roles as a software developer, software architect, and site reliability engineer across both innovative startups and established corporations.

With a keen focus on software quality, they have dedicated their career to identifying, understanding, and eliminating undefined behavior in software systems. This expertise has been honed through years of navigating the complex challenges that arise when code ventures into uncharted territories of language specifications and implementation details.

As a contractor who has worked in diverse technical environments, they've developed unique insights into how undefined behavior manifests across different codebases, teams, and organizational cultures. Their approach combines rigorous theoretical understanding with practical strategies for building robust, predictable software systems that stand the test of time and scale.

Table of Contents

    • Preface
      • Structure of This Book
      • Who This Book Is For
      • What You’ll Gain
    • Introduction
      • Why Should You Care?
      • Undefined vs. Unexpected behavior
    • Foundations
      • Basic Concepts and Definitions
      • The Contract Model
      • Chapter Exercises
    • Performance Implications of Undefined Behavior
      • The Optimization Dilemma
      • Case Study: Loop Optimization Through Undefined Behavior
      • Measuring the Performance Impact
      • The Performance Benefit of Assuming No Undefined Behavior
      • Performance vs. Safety: Modern Approaches
      • Performance Profiling of Safety Features
      • Real-World Optimization Strategies
      • SIMD and Vectorization
      • Conclusion
      • Chapter Exercises
    • Security Implications of Undefined Behavior
      • The Security-Behavior Connection
      • Common Vulnerability Classes Rooted in Undefined Behavior
      • Real-World Security Exploits
      • Exploit Development: From Undefined Behavior to Code Execution
      • Architectural Patterns to Mitigate Security Risks
      • Security-Focused Testing Strategies
      • Secure Development Practices
      • Case Study: OpenSSL Heartbleed Post-Mortem
      • Conclusion
      • Chapter Exercises
    • Undefined Behavior in Embedded Systems
      • The Critical Nature of Embedded Systems
      • Common Sources of Undefined Behavior in Embedded Systems
      • Real-Time Operating Systems (RTOS) and Undefined Behavior
      • Memory Management in Embedded Systems
      • Hardware Peripherals and Undefined Behavior
      • Power Management and Undefined Behavior
      • Safety-Critical Systems Approach
      • Boot Process and Initialization
      • Case Study: Toyota Unintended Acceleration
      • Best Practices for Embedded Systems
      • Embedded Systems Testing Strategies
      • Future Trends in Embedded Systems
      • Conclusion
      • Chapter Exercises
    • Testing to Reduce Undefined Behavior
      • The Role of Testing in Preventing Undefined Behavior
      • Unit Testing: The First Line of Defense
      • Integration Testing
      • Security Testing
      • Advanced Testing Techniques
      • Test Coverage and Quality Metrics
      • Validating the Test Suite Itself
      • Continuous Integration and Testing
      • Contract Testing
      • Test Data Management
      • Best Practices for Effective Testing
      • Real-World Example: Testing a Financial Transaction Service
      • Conclusion
      • Chapter Exercises
    • Formal Verification: Mathematically Proving Absence of Undefined Behavior
      • Introduction to Formal Verification
      • Formal Methods Approaches
      • Formal Verification Tools
      • Verifying Absence of Specific Undefined Behaviors
      • Real-World Applications of Formal Verification
      • Integrating Formal Verification into Development
      • Case Study: Verifying a Security Protocol
      • Verification Challenges and Limitations
      • The Future of Formal Verification
      • Practical Steps for Adopting Formal Verification
      • Conclusion
      • Chapter Exercises
    • Undefined Behavior in C
      • Introduction to C’s Undefined Behavior
      • Common Sources of Undefined Behavior in C
      • Compiler Optimizations and Undefined Behavior
      • Strategies for Avoiding Undefined Behavior in C
      • C23 and the Future of Undefined Behavior
      • Chapter Exercises
    • Undefined Behavior in C++
      • Introduction to C++’s Undefined Behavior
      • Common Sources of Undefined Behavior in C++
      • C++ Specific Undefined Behaviors
      • Modern C++ Solutions to Undefined Behavior
      • Libraries and Tools for C++ Safety
      • Chapter Exercises
    • Rust: Building Confidence Through Language Design
      • Introduction to Rust’s Safety Philosophy
      • Ownership: The Foundation of Rust’s Safety
      • Memory Safety Without Garbage Collection
      • Preventing Common Sources of Undefined Behavior
      • The Unsafe Escape Hatch
      • Compile-Time Guarantees
      • Interoperability with C and C++
      • Debugging and Tools for Safe Rust
      • Rust Compared to Other Languages
      • Real-World Examples: Preventing Bugs with Rust
      • Rust’s Limitations
      • Embracing Safe Programming with Rust
      • Chapter Exercises
      • Conclusion
    • Unexpected Behavior in Python
      • Introduction to Python’s Behavior Model
      • The Distinction: Implementation-Defined vs. Unexpected
      • Common Sources of Unexpected Behavior in Python
      • Type-Related Unexpected Behaviors
      • Security Implications of Unexpected Behavior
      • Debugging and Prevention Strategies
      • Chapter Exercises
    • Unexpected Behavior in Java
      • Introduction to Java’s Behavior Model
      • Java’s Safety Guarantees
      • Java 21: Modern Immutability Features
      • Common Sources of Unexpected Behavior in Java
      • Equality and Identity
      • Security Implications
      • Strategies for Safer Java Programming with Java 21
      • Chapter Exercises
    • Unexpected Behavior in JavaScript
      • Introduction to JavaScript’s Behavior Model
      • Type Coercion and Equality
      • Scope and Hoisting
      • The this Keyword
      • Asynchronous Behavior
      • Array Behavior
      • Object Property Behavior
      • Floating-Point Math
      • Security Implications
      • Modern JavaScript and Mitigation Strategies
      • Chapter Exercises
    • TypeScript: Preventing Unexpected Behavior
      • Introduction to TypeScript’s Type System
      • How TypeScript Helps Prevent JavaScript Surprises
      • TypeScript’s Escape Hatches and Their Dangers
      • TypeScript-Specific Pitfalls
      • Advanced TypeScript Type Safety
      • Integration with JavaScript
      • Security Implications
      • Best Practices for TypeScript Type Safety
      • Chapter Exercises
    • Ada: Designing Out Undefined Behavior
      • Introduction to Ada’s Safety Philosophy
      • Core Safety Features of Ada
      • Memory Safety in Ada
      • Concurrency Safety
      • Safe Exceptions Instead of Undefined Behavior
      • Contracts and Formal Verification
      • SPARK: Taking Safety to the Next Level
      • Real-World Example: The IRONSIDES DNS Server
      • Comparison with Undefined Behavior in Other Languages
      • Costs and Trade-offs
      • Chapter Exercises
    • Haskell: Type Safety and Referential Transparency
      • Introduction to Haskell’s Safety Philosophy
      • Core Safety Features of Haskell
      • Memory Safety in Haskell
      • Type Classes: Principled Polymorphism
      • Managing Effects with Types
      • Handling Failures with Monads
      • Safe Concurrency with Pure Functions
      • Preventing Common Issues with Advanced Types
      • Property-Based Testing
      • Formal Verification with Dependent Types
      • Comparison with Undefined Behavior in Other Languages
      • Costs and Trade-offs
      • Real-World Example: Facebook’s Sigma Anti-Spam System
      • Chapter Exercises
    • Cross-Language Comparison of Undefined Behavior
      • Introduction
      • Spectrum of Behavior Guarantees
      • Language Family Comparison
      • Common Sources of UB Across Languages
      • Practical Implications for Polyglot Programmers
      • Security Implications of Cross-Language Differences
      • Performance Tradeoffs vs. Safety Guarantees
      • Case Study: A Cross-Language Vulnerability
      • Conclusion
      • Exercises
    • Language Migration: Navigating Undefined Behavior Across Paradigms
      • Introduction
      • Strategic Considerations for Language Migration
      • Migration Paths and Their Challenges
      • Practical Migration Techniques
      • Case Studies: Successful Migration Stories
      • Common Pitfalls and How to Avoid Them
      • Migration Decision Framework
      • Tools to Support Migration
      • Philosophical Considerations
      • Conclusion
      • Exercises
    • Appendix: Reference Tables
      • Common Undefined Behaviors in C and C++
      • Common Undefined Behaviors in Java
      • Common Undefined Behaviors in Python
      • Common Undefined Behaviors in JavaScript
      • Real-World Security Exploits Caused by Undefined Behavior
      • Behavior Comparison Across Languages
      • Tools for Detecting Undefined Behavior
      • CERT Secure Coding Standards
      • Standards and Specifications
      • Glossary of Terms

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub