boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

270-unsigned-overflow-cmp.c (731B)


      1 /* tests/cc/270-unsigned-overflow-cmp.c — arithmetic on unsigned narrow
      2  * types must wrap modulo 2^width before being used in comparisons.
      3  * cg-binop emits 64-bit ALU ops and spills the unmasked result to a
      4  * frame rval slot; reload of a frame rval uses %cg-emit-ld-slot (8-byte
      5  * untyped load), so the overflowed high bits leak past width and corrupt
      6  * comparisons against the wrapped value. Returns 0 when correct. */
      7 int main(void) {
      8     unsigned int a = 0xFFFFFFFFu;
      9     unsigned int b = 1u;
     10     /* (a + b) wraps to 0u; (0u != 0) must be false. With the bug the
     11      * sum's bit 32 survives the spill/reload and the compare returns 1. */
     12     if ((a + b) != 0u) return 1;
     13     if ((a + b) >= 1u) return 2;
     14     return 0;
     15 }