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 <stefanb@linux.ibm.com>
This commit is contained in:
Stefan Berger 2024-07-11 16:37:57 -04:00 committed by Stefan Berger
parent d7f5fc3644
commit 5af7abe1f6

View File

@ -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]);