From d4cf93c2cfd8076b00ac592375645f63bae7b158 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 5 Jul 2024 13:17:53 -0600 Subject: [PATCH] avoid overflow if clamping a digit with used size 0 --- wolfssl/wolfcrypt/sp_int.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 514b7110f..ba1689561 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -695,9 +695,11 @@ typedef struct sp_ecc_ctx { #define sp_clamp(a) \ do { \ int ii; \ - for (ii = (int)(a)->used - 1; ii >= 0 && (a)->dp[ii] == 0; ii--) { \ + if ((a)->used > 0) { \ + for (ii = (int)(a)->used - 1; ii >= 0 && (a)->dp[ii] == 0; ii--) { \ + } \ + (a)->used = (unsigned int)ii + 1; \ } \ - (a)->used = (unsigned int)ii + 1; \ } while (0) /* Check the compiled and linked math implementation are the same.