libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / lib / hcrypto / libtommath / bn_mp_init_multi.c
blobd8390b5a033567f4df3a7dbb68131256651add11
1 #include "tommath_private.h"
2 #ifdef BN_MP_INIT_MULTI_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 #include <stdarg.h>
8 mp_err mp_init_multi(mp_int *mp, ...)
10 mp_err err = MP_OKAY; /* Assume ok until proven otherwise */
11 int n = 0; /* Number of ok inits */
12 mp_int *cur_arg = mp;
13 va_list args;
15 va_start(args, mp); /* init args to next argument from caller */
16 while (cur_arg != NULL) {
17 if (mp_init(cur_arg) != MP_OKAY) {
18 /* Oops - error! Back-track and mp_clear what we already
19 succeeded in init-ing, then return error.
21 va_list clean_args;
23 /* now start cleaning up */
24 cur_arg = mp;
25 va_start(clean_args, mp);
26 while (n-- != 0) {
27 mp_clear(cur_arg);
28 cur_arg = va_arg(clean_args, mp_int *);
30 va_end(clean_args);
31 err = MP_MEM;
32 break;
34 n++;
35 cur_arg = va_arg(args, mp_int *);
37 va_end(args);
38 return err; /* Assumed ok, if error flagged above. */
41 #endif