Skip to the content ForoHackers
Log inJoin

undefined_behaviour_enjoyer

Joined 22 November 2024

Posts in the forum

  1. Replied in Shadow stacks and CFI, explained with a CTF mindset

    And in C and C++ the root cause is often undefined behaviour that the compiler then "optimizes" in surprising ways. Fix the bug and the mitigation becomes insurance instead of the only thing between …

  2. Replied in Why comparing secrets with == leaks them, in twenty lines

    About "a compiler is free to be clever": yes. The C abstract machine knows nothing about time, so the optimizer is allowed to turn your branch-free loop back into an early exit if it proves the resul…

  3. Replied in x + 1 > x: a function the compiler turns into `return 1`

    Excellent, and the standard-lawyer footnote: the compiler is not required to produce "return 1" either. Anything is allowed, which is exactly why "it worked in testing" proves nothing. The one thing …

  4. Started comptime, templates and constexpr: three answers to "run code at compile time"

    Three languages, three different answers to the same wish: do some computation before the program runs. **C++ templates.** A separate sublanguage, functional, evaluated by the compiler by pattern ma…

  5. Replied in Hago un juego indie en C++ y mi primer bug fue un puntero colgante

    Also: "never failed on my machine, failed on my friend's" is the signature of undefined behaviour. Reading freed memory is allowed to do anything, including looking fine for weeks. ASan and a debug a…

  6. Replied in fma: when a*b+c rounds once instead of twice

    Correct. Compare with a tolerance measured in ulps, or accept a relative error you can justify, and keep a bit-exact test only when you control the whole toolchain.

  7. Started fma: when a*b+c rounds once instead of twice

    `a * b + c` looks like two operations, and in plain IEEE arithmetic it is: multiply, round to the nearest double, add, round again. Most modern CPUs and GPUs also have a **fused multiply-add** that c…

  8. Replied in Adding the same floats in a different order gives a different answer

    And `-ffast-math` is the classic "I asked for it" trap: it lets the compiler assume no NaNs or infinities and reassociate freely, so your carefully written Kahan summation can be optimised into a pla…

  9. Replied in A ray tracer in 40 lines: what the key lines are doing

    The `1e-4` is the part people skip, and then they get "shadow acne": the secondary ray starts on the surface, floating point says it is slightly inside, and the sphere shadows itself in speckles. A f…

  10. Replied in Hello. Your compiler is smarter than your loop, and it is right.

    Exactly. `__builtin_add_overflow` exists for precisely that case, and it compiles to the CPU's overflow flag. Defined behaviour, one instruction. No excuse.

  11. Started Hello. Your compiler is smarter than your loop, and it is right.

    Short intro: I collect undefined behaviour the way other people collect stamps, and I read compiler optimisation passes for fun. I work in C and C++ and mostly on sanitizers and static analysis. A s…