Merge pull request #192 from libtom/alloc-sizes allocation functions: pass size to XREALLOC and XFREE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
diff --git a/bn_mp_clear.c b/bn_mp_clear.c
index 1f360b2..b8e724c 100644
--- a/bn_mp_clear.c
+++ b/bn_mp_clear.c
@@ -25,7 +25,7 @@ void mp_clear(mp_int *a)
}
/* free ram */
- XFREE(a->dp);
+ XFREE(a->dp, sizeof (mp_digit) * (size_t)a->alloc);
/* reset members to make debugging easier */
a->dp = NULL;
diff --git a/bn_mp_fwrite.c b/bn_mp_fwrite.c
index 3bcb445..85a942f 100644
--- a/bn_mp_fwrite.c
+++ b/bn_mp_fwrite.c
@@ -28,18 +28,18 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
}
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
- XFREE(buf);
+ XFREE(buf, len);
return err;
}
for (x = 0; x < len; x++) {
if (fputc((int)buf[x], stream) == EOF) {
- XFREE(buf);
+ XFREE(buf, len);
return MP_VAL;
}
}
- XFREE(buf);
+ XFREE(buf, len);
return MP_OKAY;
}
#endif
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index 1394451..b120194 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -29,7 +29,9 @@ int mp_grow(mp_int *a, int size)
* in case the operation failed we don't want
* to overwrite the dp member of a.
*/
- tmp = (mp_digit *) XREALLOC(a->dp, (size_t)size * sizeof(mp_digit));
+ tmp = (mp_digit *) XREALLOC(a->dp,
+ (size_t)a->alloc * sizeof (mp_digit),
+ (size_t)size * sizeof(mp_digit));
if (tmp == NULL) {
/* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM;
diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c
index fdd5140..0ca29ec 100644
--- a/bn_mp_prime_random_ex.c
+++ b/bn_mp_prime_random_ex.c
@@ -123,7 +123,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
err = MP_OKAY;
error:
- XFREE(tmp);
+ XFREE(tmp, bsize);
return err;
}
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index b714fdd..fa30184 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -23,7 +23,9 @@ int mp_shrink(mp_int *a)
}
if (a->alloc != used) {
- if ((tmp = (mp_digit *) XREALLOC(a->dp, (size_t)used * sizeof(mp_digit))) == NULL) {
+ if ((tmp = (mp_digit *) XREALLOC(a->dp,
+ (size_t)a->alloc * sizeof (mp_digit),
+ (size_t)used * sizeof(mp_digit))) == NULL) {
return MP_MEM;
}
a->dp = tmp;
diff --git a/tommath_private.h b/tommath_private.h
index 15c2338..057f878 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -29,14 +29,14 @@ extern "C" {
/* define heap macros */
#ifndef XMALLOC
/* default to libc stuff */
-# define XMALLOC malloc
-# define XFREE free
-# define XREALLOC realloc
+# define XMALLOC(size) malloc(size)
+# define XFREE(mem, size) free(mem)
+# define XREALLOC(mem, oldsize, newsize) realloc(mem, newsize)
#else
/* prototypes for our heap functions */
-extern void *XMALLOC(size_t n);
-extern void *XREALLOC(void *p, size_t n);
-extern void XFREE(void *p);
+extern void *XMALLOC(size_t size);
+extern void *XREALLOC(void *mem, size_t oldsize, size_t newsize);
+extern void XFREE(void *mem, size_t size);
#endif
/* ---> Basic Manipulations <--- */