Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The compiler is allowed to assume that if x and y are signed ints:

   if (x < 0 || y < 0) return -1;
   return x * y;
Will not overflow. And if you try to check for overflow with:

   if (x < 0 || y < 0) return -1;
   if (x * y < 0) return -2;
   return x*y;
Then yes, the compiler is within spec to remove your check because the only situation in which you could hit that check would be after signed integer overflow, which it is allowed to assume won't happen.

One way to implement this check in GCC where the compiler will respect it would be:

   if (x < 0 || y < 0) return -1;
   int z;
   if (__builtin_smul_overflow(x, y, &z)) return -2;
   return z;


Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: