Coverage Report

Created: 2023-06-07 06:45

/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
270k
{
9
270k
   int     i;
10
270k
   mp_digit *tmp;
11
12
   /* if the alloc size is smaller alloc more ram */
13
270k
   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
267k
      tmp = (mp_digit *) MP_REALLOC(a->dp,
21
267k
                                    (size_t)a->alloc * sizeof(mp_digit),
22
267k
                                    (size_t)size * sizeof(mp_digit));
23
267k
      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
267k
      a->dp = tmp;
30
31
      /* zero excess digits */
32
267k
      i        = a->alloc;
33
267k
      a->alloc = size;
34
267k
      MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
35
267k
   }
36
270k
   return MP_OKAY;
37
270k
}
38
#endif