Fix overflows in shift primitives.
[u/mdw/catacomb] / mpx.c
diff --git a/mpx.c b/mpx.c
index b88bd49..d7ea70a 100644 (file)
--- a/mpx.c
+++ b/mpx.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mpx.c,v 1.13 2002/10/19 17:56:50 mdw Exp $
+ * $Id: mpx.c,v 1.14 2002/10/19 18:55:08 mdw Exp $
  *
  * Low-level multiprecision arithmetic
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mpx.c,v $
+ * Revision 1.14  2002/10/19 18:55:08  mdw
+ * Fix overflows in shift primitives.
+ *
  * Revision 1.13  2002/10/19 17:56:50  mdw
  * Fix bit operations.  Test them (a bit) better.
  *
@@ -470,8 +473,12 @@ void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
   /* --- Handle a shift by a multiple of the word size --- */
 
   if (nb == 0) {
-    MPX_COPY(dv + nw, dvl, av, avl);
-    memset(dv, 0, MPWS(nw));
+    if (nw >= dvl - dv)
+      MPX_ZERO(dv, dvl);
+    else {
+      MPX_COPY(dv + nw, dvl, av, avl);
+      memset(dv, 0, MPWS(nw));
+    }
   }
 
   /* --- And finally the difficult case --- *
@@ -562,8 +569,12 @@ void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
 
   /* --- Handle a shift by a multiple of the word size --- */
 
-  if (nb == 0)
-    MPX_COPY(dv, dvl, av + nw, avl);
+  if (nb == 0) {
+    if (nw >= avl - av)
+      MPX_ZERO(dv, dvl);
+    else
+      MPX_COPY(dv, dvl, av + nw, avl);
+  }
 
   /* --- And finally the difficult case --- */
 
@@ -572,7 +583,7 @@ void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
     size_t nr = MPW_BITS - nb;
 
     av += nw;
-    w = *av++;
+    w = av < avl ? *av++ : 0;
     while (av < avl) {
       mpw t;
       if (dv >= dvl)