From 55e13d40b3fb1754d98b1aefa94e65f981a1174f Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Tue, 12 Apr 2022 08:53:16 +1000 Subject: [PATCH] SP Math sp_read_radix_10: check _sp_add_d doesn't error due to overflow _sp_add_d: Don't change value of used for result until it is known to be valid. --- wolfcrypt/src/sp_int.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 284b6bc6a..43a85a48f 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -5219,11 +5219,13 @@ static int _sp_add_d(sp_int* a, sp_int_digit d, sp_int* r) } } if (i == a->used) { - r->used++; - if (i < r->size) + if (i < r->size) { + r->used++; r->dp[i] = 1; - else + } + else { err = MP_VAL; + } } } if (err == MP_OKAY) { @@ -14889,7 +14891,10 @@ static int _sp_read_radix_10(sp_int* a, const char* in) if (err != MP_OKAY) { break; } - (void)_sp_add_d(a, ch, a); + err = _sp_add_d(a, ch, a); + if (err != MP_OKAY) { + break; + } } #ifdef WOLFSSL_SP_INT_NEGATIVE if ((err == MP_OKAY) && sp_iszero(a)) {