diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 40ad95104..752f37cb3 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -5181,10 +5181,13 @@ int sp_set_int(sp_int* a, unsigned long n) else { int i; - for (i = 0; n > 0; i++,n >>= SP_WORD_SIZE) { + for (i = 0; (i < a->size) && (n > 0); i++,n >>= SP_WORD_SIZE) { a->dp[i] = (sp_int_digit)n; } a->used = i; + if ((i == a->size) && (n != 0)) { + err = MP_VAL; + } } #endif #ifdef WOLFSSL_SP_INT_NEGATIVE