From 5af7abe1f6dc7ec2bd4e3efa1742a83d00cac762 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Thu, 11 Jul 2024 16:37:57 -0400 Subject: [PATCH] tpm2: Filter bad input values to avoid underflow in FindNthSetBit (Coverity) Address the following Coverity complaint (1550494) by filtering out bad input values: "Expression i--, which is equal to 65535, where i is known to be equal to 0, underflows the type that receives it, an unsigned integer 16 bits wide." aSize is typcially 2048 and n is always >= 1 per the input parameter. Therefore no side-effects are expected from this filter. Signed-off-by: Stefan Berger --- src/tpm2/crypto/openssl/CryptPrimeSieve.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tpm2/crypto/openssl/CryptPrimeSieve.c b/src/tpm2/crypto/openssl/CryptPrimeSieve.c index 8c4e52b0..1e8cea10 100644 --- a/src/tpm2/crypto/openssl/CryptPrimeSieve.c +++ b/src/tpm2/crypto/openssl/CryptPrimeSieve.c @@ -215,6 +215,10 @@ FindNthSetBit( int retValue; UINT32 sum = 0; BYTE sel; + + if (n < 1 || aSize < 1) // libtpms added begin: Coverity 1550494 + return -1; // libtpms end + //find the bit for(i = 0; (i < (int)aSize) && (sum < n); i++) sum += BitsInByte(a[i]);