division by zero optimized away

Xi Wang xi.wang at gmail.com
Sat Jul 14 22:46:21 CEST 2012


There are two occurrences of intentional division by zero in mpi.
Division by zero is undefined behavior in C, and it may not work
as intended.

1) Division by zero doesn't "provoke a signal" on architectures
   like PowerPC.

2) C compilers like clang will optimize away these divisions, even
   though the code tries "to make the compiler not remove" them.

gcry_mpi_powm() at mpi/mpi-pow.c:78

  if (!msize)
    msize = 1 / msize;      /* Provoke a signal.  */

_gcry_mpih_divrem() at mpi/mpih-div.c:213

    switch(dsize) {
      case 0:
        /* We are asked to divide by zero, so go ahead and do it!  (To make
           the compiler not remove this statement, return the value.)  */
        return 1 / dsize;

How about using something like signal(SIGFPE) instead?

- xi

More information about the Gcrypt-devel mailing list