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 cbf8475b93
(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 <makejian@xiaomi.com>
This commit is contained in:
makejian 2023-07-04 22:02:35 +08:00 committed by Xiang Xiao
parent 5e0e027880
commit b6c2362c6a
1 changed files with 7 additions and 3 deletions

View File

@ -435,9 +435,7 @@ int crypto_unregister(uint32_t driverid, int alg)
/* Sanity checks. */ /* Sanity checks. */
if (driverid >= crypto_drivers_num || crypto_drivers == NULL || if (driverid >= crypto_drivers_num || crypto_drivers == NULL ||
((alg <= 0 || alg > CRYPTO_ALGORITHM_MAX) && alg <= 0 || alg > (CRYPTO_ALGORITHM_MAX + 1))
alg != CRYPTO_ALGORITHM_MAX + 1) ||
crypto_drivers[driverid].cc_alg[alg] == 0)
{ {
nxmutex_unlock(&g_crypto_lock); nxmutex_unlock(&g_crypto_lock);
return -EINVAL; return -EINVAL;
@ -445,6 +443,12 @@ int crypto_unregister(uint32_t driverid, int alg)
if (alg != CRYPTO_ALGORITHM_MAX + 1) 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; crypto_drivers[driverid].cc_alg[alg] = 0;
/* Was this the last algorithm ? */ /* Was this the last algorithm ? */