Coverage Report

Created: 2023-09-25 06:08

/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
1.24k
{
9
1.24k
   int     i;
10
1.24k
   mp_digit *tmp;
11
12
1.24k
   if (size < 0) {
13
0
      return MP_VAL;
14
0
   }
15
16
   /* if the alloc size is smaller alloc more ram */
17
1.24k
   if (a->alloc < size) {
18
      /* reallocate the array a->dp
19
       *
20
       * We store the return in a temporary variable
21
       * in case the operation failed we don't want
22
       * to overwrite the dp member of a.
23
       */
24
1.24k
      tmp = (mp_digit *) MP_REALLOC(a->dp,
25
1.24k
                                    (size_t)a->alloc * sizeof(mp_digit),
26
1.24k
                                    (size_t)size * sizeof(mp_digit));
27
1.24k
      if (tmp == NULL) {
28
         /* reallocation failed but "a" is still valid [can be freed] */
29
0
         return MP_MEM;
30
0
      }
31
32
      /* reallocation succeeded so set a->dp */
33
1.24k
      a->dp = tmp;
34
35
      /* zero excess digits */
36
1.24k
      i        = a->alloc;
37
1.24k
      a->alloc = size;
38
1.24k
      MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
39
1.24k
   }
40
1.24k
   return MP_OKAY;
41
1.24k
}
42
#endif