libtom / tomsfastmath

TomsFastMath is a fast public domain, open source, large integer arithmetic library written in portable ISO C.
http://www.libtom.net
Other
213 stars 66 forks source link

Now truncating input array if ENDIANESS is unknown #15

Closed bchauvaux closed 7 years ago

bchauvaux commented 7 years ago

This fix addresses the case where the input array is longer than the fp_int capacity and ENDIANNESS is not specified... in which case the existing code would overflow fp_int

sjaeckel commented 7 years ago

I think we can accept this PR, as this makes the behavior of fp_read_unsigned_bin() consistent whether or not the endianness is known.

...but the description needs to be fixed as there could never be an overflow...

sjaeckel commented 7 years ago

...but the description needs to be fixed as there could never be an overflow...

damn, I was wrong, the last iteration would indeed overflow dp...

sjaeckel commented 7 years ago

Would this change probably make sense too?

@@ -13,7 +13,7 @@
 void fp_mul_2d(fp_int *a, int b, fp_int *c)
 {
    fp_digit carry, carrytmp, shift;
-   int x;
+   int x, limit;

    /* copy it */
    fp_copy(a, c);
@@ -28,7 +28,8 @@ void fp_mul_2d(fp_int *a, int b, fp_int *c)
    if (b != 0) {
       carry = 0;   
       shift = DIGIT_BIT - b;
-      for (x = 0; x < c->used; x++) {
+      limit = MIN(c->used, FP_SIZE);
+      for (x = 0; x < limit; x++) {
           carrytmp = c->dp[x] >> shift;
           c->dp[x] = (c->dp[x] << b) + carry;
           carry = carrytmp;
bchauvaux commented 7 years ago

Yes it would but this is one of the many location where ->used is ... "used". There are few other locations where ->used is increased without verification.