Comment

Comment by philosopher1234

>For example, a common thing programmers expect is that you can test for signed integer overflow by checking whether the result is less than one of the operands, as in this program:

    #include <stdio.h>

    int f(int x) {
        if(x+100 < x)
            printf("overflow\n");
        return x+100;
    }
>Clang optimizes away the if statement. The justification is that since signed integer overflow is undefined behavior, the compiler can assume it never happens, so x+100 must never be less than x. Ironically, this program would correctly detect overflow on both ones'-complement and two's-complement machines if the compiler would actually emit the check.

My god…

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton