math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / mpint.h
index 7867f6a..e351b05 100644 (file)
 
 #include <limits.h>
 
+#include <mLib/macros.h>
+
 #ifndef CATACOMB_MP_H
 #  include "mp.h"
 #endif
 
 /*----- Generic translation macros ----------------------------------------*/
 
+/* --- Warning damage control --- *
+ *
+ * GCC (at least) isn't clever enough to work out that the division in
+ * @MP_FROMINT@ is actually safe (since it will only be executed if @_i >
+ * MPW_MAX@, which would prove that @(type)MPW_MAX + 1 != 0@).
+ */
+
 /* --- @MP_FROMINT@ --- *
  *
  * Arguments:  @d@ = destination multiprecision integer
@@ -71,7 +80,9 @@
       if (_i <= MPW_MAX)                                               \
        break;                                                          \
       else                                                             \
-       _i /= (type)MPW_MAX + 1;                                        \
+       MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero"), {            \
+         _i /= (type)MPW_MAX + 1;                                      \
+       });                                                             \
     }                                                                  \
   } else {                                                             \
     _d->f |= MP_NEG;                                                   \
@@ -84,7 +95,9 @@
       if (_i >= -MPW_MAX)                                              \
        break;                                                          \
       else                                                             \
-       _i /= (type)MPW_MAX + 1;                                        \
+       MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero"), {            \
+         _i /= (type)MPW_MAX + 1;                                      \
+       });                                                             \
     }                                                                  \
   }                                                                    \
                                                                        \