tpm2: Use BITS_TO_BYTES in NVMarshal.c for calculating array sizes

Use the BITS_TO_BYTES macro where the number of bits is used for
calculating the size of a byte array.

Also, make the clearing of the rest of the byte arrays (ppList and
auditCommands) a bit more efficient than clearing the whole array
before copying the new data into it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
This commit is contained in:
Stefan Berger 2026-03-05 09:15:16 -05:00 committed by Stefan Berger
parent 747e279530
commit 190e72838f

View File

@ -4087,7 +4087,7 @@ static UINT16
PERSISTENT_DATA_PPList_Marshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size,
UINT16 blob_version, UINT32 commandCount)
{
UINT8 ppList[(110 + 7) / 8];
UINT8 ppList[BITS_TO_BYTES(110)];
UINT16 array_size;
UINT16 written;
UINT8 *ptr;
@ -4140,8 +4140,11 @@ PERSISTENT_DATA_PPList_Unmarshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *si
if (array_size > sizeof(data->ppList))
array_size = sizeof(data->ppList);
memset(data->ppList, 0, sizeof(data->ppList));
memcpy(data->ppList, buf, array_size);
/* clear the rest of byte array */
MUST_BE(sizeof(data->ppList[0]) == sizeof(BYTE));
while (array_size < ARRAY_SIZE(data->ppList))
data->ppList[array_size++] = 0;
}
}
}
@ -4152,7 +4155,7 @@ static UINT16
PERSISTENT_DATA_AuditCommands_Marshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size,
UINT16 blob_version, UINT32 commandCount)
{
UINT8 auditCommands[(110 + 1 + 7) / 8];
UINT8 auditCommands[BITS_TO_BYTES(110 + 1)];
UINT16 array_size;
UINT16 written;
UINT8 *ptr;
@ -4164,7 +4167,7 @@ PERSISTENT_DATA_AuditCommands_Marshal(PERSISTENT_DATA *data, BYTE **buffer, INT3
* was using a COMPRESSED_LIST.
*/
assert(commandCount <= 110);
array_size = ((commandCount + 1) + 7) / 8; /* same as in Global.h PERSISTENT_DATA */
array_size = BITS_TO_BYTES(commandCount + 1); /* same as in Global.h PERSISTENT_DATA */
assert(sizeof(auditCommands) >= array_size);
ConvertToCompressedBitArray(data->auditCommands, sizeof(data->auditCommands),
auditCommands, array_size);
@ -4206,8 +4209,11 @@ PERSISTENT_DATA_AuditCommands_Unmarshal(PERSISTENT_DATA *data, BYTE **buffer, IN
if (array_size > sizeof(data->auditCommands))
array_size = sizeof(data->auditCommands);
memset(data->auditCommands, 0, sizeof(data->auditCommands));
memcpy(data->auditCommands, buf, array_size);
/* clear the rest of byte array */
MUST_BE(sizeof(data->auditCommands[0]) == sizeof(BYTE));
while (array_size < ARRAY_SIZE(data->auditCommands))
data->auditCommands[array_size++] = 0;
}
}
}