From b6c2362c6a3b5e4507ae53fc04c6cc8efc7fbf24 Mon Sep 17 00:00:00 2001 From: makejian Date: Tue, 4 Jul 2023 22:02:35 +0800 Subject: [PATCH] crypto: fix above array bounds warning in nuttx crypto crypto.c:440:38: warning: array subscript 24 is above array bounds of 'int[24]' [-Warray-bounds] 440 | crypto_drivers[driverid].cc_alg[alg] == 0) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~ In file included from crypto.c:37: nuttx/include/crypto/cryptodev.h:269:7: note: while referencing 'cc_alg' 269 | int cc_alg[CRYPTO_ALGORITHM_MAX + 1]; following commit https://github.com/openbsd/src/commit/cbf8475b9318827d4174f247f210e07ba24f7ac2 (1)alg need to blong to [1, CRYPTO_ALGORITHM_MAX + 1] in sanity checks (2)clear alg algorithm when alg blongs to [1, CRYPTO_ALGORITHM_MAX + 1) (3)clear all algorithms when alg equals to CRYPTO_ALGORITHM_MAX + 1 Signed-off-by: makejian --- crypto/crypto.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crypto/crypto.c b/crypto/crypto.c index 5c58629cdb..80550c889b 100644 --- a/crypto/crypto.c +++ b/crypto/crypto.c @@ -435,9 +435,7 @@ int crypto_unregister(uint32_t driverid, int alg) /* Sanity checks. */ if (driverid >= crypto_drivers_num || crypto_drivers == NULL || - ((alg <= 0 || alg > CRYPTO_ALGORITHM_MAX) && - alg != CRYPTO_ALGORITHM_MAX + 1) || - crypto_drivers[driverid].cc_alg[alg] == 0) + alg <= 0 || alg > (CRYPTO_ALGORITHM_MAX + 1)) { nxmutex_unlock(&g_crypto_lock); return -EINVAL; @@ -445,6 +443,12 @@ int crypto_unregister(uint32_t driverid, int alg) if (alg != CRYPTO_ALGORITHM_MAX + 1) { + if (crypto_drivers[driverid].cc_alg[alg] == 0) + { + nxmutex_unlock(&g_crypto_lock); + return -EINVAL; + } + crypto_drivers[driverid].cc_alg[alg] = 0; /* Was this the last algorithm ? */