Skip to content

allocation functions: pass size to XREALLOC and XFREE #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bn_mp_clear.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions bn_mp_fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion bn_mp_grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_prime_random_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion bn_mp_shrink.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions tommath_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <--- */
Expand Down