Login
You're viewing the techdon.dev public feed.
  • Feb 10, 2026, 12:06 PM

    Interested in compiler bugs? Here's one for x86 in e.g. gcc-14.2.0 -O1 -m32: void foo(const void *x) { if (isnan(*(float *) x)) printf("%x\n",*(int *) x); } int main() { int u = 0x7f987654; printf("%x\n",u); foo(&u); return 0; } (using math.h, stdio.h). Workaround: volatile.

    💬 1🔄 0⭐ 0

Replies

  • 💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Feb 10, 2026, 12:39 PM

    @djb @wingo You don't have a NaN, you have an object of type int. You take its address (fine), convert it to void * (fine), then convert that to float * (dubious). I'm pretty sure the C standard says that converting from void * to float * is fine, but it says nothing about what that resulting pointer actually points to, so dereferencing that may already be UB by omission.

    And accessing an object of type int via an lvalue that is not compatible with int is straight up listed as undefined behavior in C99 J.2.

    💬 1🔄 0⭐ 0
  • Feb 10, 2026, 12:53 PM

    I think you're misdiagnosing the bug here. Here's a union version of the same thing: typedef union { float f; int i; } fi;
    void foo(const fi *x) { if (isnan(x->f)) printf("%x\n",x->i); }
    int main() { fi x; x.i = 0x7f987654; printf("%x\n",x.i); foo(&x); return 0; }

    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0