Skip to content

Commit db9a47d

Browse files
committed
allocation functions: pass size to XREALLOC and XFREE
This is similar to the signatures of the custom allocation functions provided by GMP. The allocation sizes are useful if the allocator has no easy way to access the allocation size.
1 parent 32b3351 commit db9a47d

File tree

6 files changed

+17
-13
lines changed

6 files changed

+17
-13
lines changed

bn_mp_clear.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void mp_clear(mp_int *a)
2525
}
2626

2727
/* free ram */
28-
XFREE(a->dp);
28+
XFREE(a->dp, sizeof (mp_digit) * (size_t)a->alloc);
2929

3030
/* reset members to make debugging easier */
3131
a->dp = NULL;

bn_mp_fwrite.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
2828
}
2929

3030
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
31-
XFREE(buf);
31+
XFREE(buf, len);
3232
return err;
3333
}
3434

3535
for (x = 0; x < len; x++) {
3636
if (fputc((int)buf[x], stream) == EOF) {
37-
XFREE(buf);
37+
XFREE(buf, len);
3838
return MP_VAL;
3939
}
4040
}
4141

42-
XFREE(buf);
42+
XFREE(buf, len);
4343
return MP_OKAY;
4444
}
4545
#endif

bn_mp_grow.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ int mp_grow(mp_int *a, int size)
2929
* in case the operation failed we don't want
3030
* to overwrite the dp member of a.
3131
*/
32-
tmp = (mp_digit *) XREALLOC(a->dp, (size_t)size * sizeof(mp_digit));
32+
tmp = (mp_digit *) XREALLOC(a->dp,
33+
(size_t)a->alloc * sizeof (mp_digit),
34+
(size_t)size * sizeof(mp_digit));
3335
if (tmp == NULL) {
3436
/* reallocation failed but "a" is still valid [can be freed] */
3537
return MP_MEM;

bn_mp_prime_random_ex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
123123

124124
err = MP_OKAY;
125125
error:
126-
XFREE(tmp);
126+
XFREE(tmp, bsize);
127127
return err;
128128
}
129129

bn_mp_shrink.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ int mp_shrink(mp_int *a)
2323
}
2424

2525
if (a->alloc != used) {
26-
if ((tmp = (mp_digit *) XREALLOC(a->dp, (size_t)used * sizeof(mp_digit))) == NULL) {
26+
if ((tmp = (mp_digit *) XREALLOC(a->dp,
27+
(size_t)a->alloc * sizeof (mp_digit),
28+
(size_t)used * sizeof(mp_digit))) == NULL) {
2729
return MP_MEM;
2830
}
2931
a->dp = tmp;

tommath_private.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ extern "C" {
2929
/* define heap macros */
3030
#ifndef XMALLOC
3131
/* default to libc stuff */
32-
# define XMALLOC malloc
33-
# define XFREE free
34-
# define XREALLOC realloc
32+
# define XMALLOC(size) malloc(size)
33+
# define XFREE(mem, size) free(mem)
34+
# define XREALLOC(mem, oldsize, newsize) realloc(mem, newsize)
3535
#else
3636
/* prototypes for our heap functions */
37-
extern void *XMALLOC(size_t n);
38-
extern void *XREALLOC(void *p, size_t n);
39-
extern void XFREE(void *p);
37+
extern void *XMALLOC(size_t size);
38+
extern void *XREALLOC(void *mem, size_t oldsize, size_t newsize);
39+
extern void XFREE(void *mem, size_t size);
4040
#endif
4141

4242
/* ---> Basic Manipulations <--- */

0 commit comments

Comments
 (0)