Story Details

  • It is not a compiler error (2017)

    Posted: 2025-02-20 07:58:47

    The blog post "It is not a compiler error (2017)" explores a subtle bug related to floating-point comparisons in C++. The author demonstrates how seemingly innocuous code, involving comparing a floating-point value against zero after decrementing it in a loop, can lead to unexpected infinite loops. This arises because floating-point numbers have limited precision, and repeated subtraction of a small value from a larger one might never exactly reach zero. The post emphasizes the importance of understanding floating-point limitations and suggests using alternative comparison methods, like checking if the value is within a small tolerance of zero (epsilon comparison), or restructuring the loop condition to avoid direct equality checks with floating-point numbers.

    Summary of Comments ( 74 )
    https://news.ycombinator.com/item?id=43112187

    HN users discuss integer overflow in C/C++, focusing on its undefined behavior and the security implications. Some highlight the dangers, especially in situations where the compiler optimizes away overflow checks based on the assumption that it can't happen. Others point out that -fwrapv can enforce predictable wrapping behavior, making code safer but potentially slower. The discussion also touches on how static analyzers can help catch these issues, and the inherent difficulties in ensuring complete safety in C/C++ due to the language's flexibility. A few commenters mention alternatives like Rust, which offer stricter memory safety and overflow handling. One commenter shares a personal anecdote about an integer underflow vulnerability they found in a C++ program, emphasizing the real-world impact of these seemingly theoretical problems.