This C app crashes when it's compiled with zig

When this piece of code is compiled and executed with zig cc, it’s crashing without showing any error message.

#include <stdio.h>

int main(void) {
  unsigned int x = 1;
  x = 0 - 4.0;

  printf("%u\n", x);
}

The version I’m using is 0.13.0

Zig cc enables clang’s undefined behavior sanitizer by default.
It crashes here because -4 is not a valid unsigned integer value.
It’s weird that it doesn’t show an error message though.

If you want to disable the sanitizer, you can use the command line flag
-fno-sanitize=undefined, but I’d suggest to fix undefined behavior instead of hiding it.

2 Likes

Thank you. It took me months to find out the problem and get to this point.