virPCIVPDParseVPDLargeResourceFields: Refactor processing of read data

Use a 'switch' statement instead of a bunch of if/elseif statements.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2024-01-30 15:14:49 +01:00
parent f1deac9635
commit 378b82dac2
1 changed files with 37 additions and 29 deletions

View File

@ -485,7 +485,8 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uint16_t resPos, uint16_t re
/* Advance the position to the first byte of the next field. */
fieldPos += fieldDataLen;
if (fieldFormat == VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT) {
switch (fieldFormat) {
case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT:
/* Trim whitespace around a retrieved value and set it to be a field's value. Cases
* where unnecessary whitespace was present around a field value have been encountered
* in the wild.
@ -497,24 +498,30 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uint16_t resPos, uint16_t re
VIR_DEBUG("A value for field %s contains invalid characters", fieldKeyword);
continue;
}
} else if (fieldFormat == VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RESVD) {
break;
case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_BINARY:
fieldValue = g_malloc(fieldDataLen);
memcpy(fieldValue, buf, fieldDataLen);
break;
case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RDWR:
/* Skip the read-write space since it is used for indication only. */
hasRW = true;
goto done;
case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RESVD:
if (*csum) {
/* All bytes up to and including the checksum byte should add up to 0. */
VIR_INFO("Checksum validation has failed");
return false;
}
hasChecksum = true;
break;
} else if (fieldFormat == VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RDWR) {
/* Skip the read-write space since it is used for indication only. */
hasRW = true;
break;
} else if (fieldFormat == VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST) {
goto done;
case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST:
/* Skip unknown fields */
continue;
} else {
fieldValue = g_malloc(fieldDataLen);
memcpy(fieldValue, buf, fieldDataLen);
}
if (readOnly) {
@ -528,6 +535,7 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uint16_t resPos, uint16_t re
virPCIVPDResourceUpdateKeyword(res, readOnly, fieldKeyword, fieldValue);
}
done:
/* May have exited the loop prematurely in case RV or RW were encountered and
* they were not the last fields in the section. */
endReached = (fieldPos >= resPos + resDataLen);