sshbn.c (modmul): Prevent buffer underrun.
[u/mdw/putty] / sshbn.c
diff --git a/sshbn.c b/sshbn.c
index 24f3ca6..da24978 100644 (file)
--- a/sshbn.c
+++ b/sshbn.c
@@ -259,9 +259,6 @@ static void internal_mul(const BignumInt *a, const BignumInt *b,
         int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */
         int midlen = botlen + 1;
         BignumDblInt carry;
-#ifdef KARA_DEBUG
-        int i;
-#endif
 
         /*
          * The coefficients a_1 b_1 and a_0 b_0 just avoid overlapping
@@ -1018,6 +1015,12 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
 
     pqlen = (p[0] > q[0] ? p[0] : q[0]);
 
+    /* Make sure that we're allowing enough space.  The shifting below will
+     * underflow the vectors we allocate if `pqlen' is too small.
+     */
+    if (2*pqlen <= mlen)
+        pqlen = mlen/2 + 1;
+
     /* Allocate n of size pqlen, copy p to n */
     n = snewn(pqlen, BignumInt);
     i = pqlen - p[0];