/src/dropbear/libtommath/bn_mp_grow.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "tommath_private.h" |
2 | | #ifdef BN_MP_GROW_C |
3 | | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
4 | | /* SPDX-License-Identifier: Unlicense */ |
5 | | |
6 | | /* grow as required */ |
7 | | mp_err mp_grow(mp_int *a, int size) |
8 | 2.31M | { |
9 | 2.31M | int i; |
10 | 2.31M | mp_digit *tmp; |
11 | | |
12 | | /* if the alloc size is smaller alloc more ram */ |
13 | 2.31M | if (a->alloc < size) { |
14 | | /* reallocate the array a->dp |
15 | | * |
16 | | * We store the return in a temporary variable |
17 | | * in case the operation failed we don't want |
18 | | * to overwrite the dp member of a. |
19 | | */ |
20 | 2.27M | tmp = (mp_digit *) MP_REALLOC(a->dp, |
21 | 2.27M | (size_t)a->alloc * sizeof(mp_digit), |
22 | 2.27M | (size_t)size * sizeof(mp_digit)); |
23 | 2.27M | if (tmp == NULL) { |
24 | | /* reallocation failed but "a" is still valid [can be freed] */ |
25 | 0 | return MP_MEM; |
26 | 0 | } |
27 | | |
28 | | /* reallocation succeeded so set a->dp */ |
29 | 2.27M | a->dp = tmp; |
30 | | |
31 | | /* zero excess digits */ |
32 | 2.27M | i = a->alloc; |
33 | 2.27M | a->alloc = size; |
34 | 2.27M | MP_ZERO_DIGITS(a->dp + i, a->alloc - i); |
35 | 2.27M | } |
36 | 2.31M | return MP_OKAY; |
37 | 2.31M | } |
38 | | #endif |