dsp: platform: Merge PDR header extraction and validation

These two functions were tightly coupled, in the sense that in all
invocations of either were proximal to the other.

Merge them to avoid the spatial separation and improve both our own and
the compiler's ability to reason about the relationships.

Change-Id: I098afac06fea56a26762879a620e74539f6e6d52
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/src/dsp/platform.c b/src/dsp/platform.c
index 52caa53..7cf5853 100644
--- a/src/dsp/platform.c
+++ b/src/dsp/platform.c
@@ -15,20 +15,6 @@
 #include <string.h>
 #include <uchar.h>
 
-static int pldm_platform_pdr_hdr_validate(struct pldm_value_pdr_hdr *ctx,
-					  size_t lower, size_t upper)
-{
-	if (ctx->length + sizeof(*ctx) < lower) {
-		return PLDM_ERROR_INVALID_LENGTH;
-	}
-
-	if (ctx->length > upper) {
-		return PLDM_ERROR_INVALID_LENGTH;
-	}
-
-	return PLDM_SUCCESS;
-}
-
 LIBPLDM_ABI_STABLE
 int encode_state_effecter_pdr(
 	struct pldm_state_effecter_pdr *const effecter,
@@ -1805,16 +1791,11 @@
 		return pldm_xlate_errno(rc);
 	}
 
-	rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &pdr_value->hdr);
-	if (rc) {
-		return pldm_xlate_errno(rc);
-	}
-
-	rc = pldm_platform_pdr_hdr_validate(
-		&pdr_value->hdr, PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
+	rc = pldm_msgbuf_extract_value_pdr_hdr(
+		buf, &pdr_value->hdr, PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
 		pdr_data_length);
 	if (rc) {
-		return rc;
+		return pldm_xlate_errno(rc);
 	}
 
 	pldm_msgbuf_extract(buf, pdr_value->terminus_handle);
@@ -2787,16 +2768,11 @@
 		return pldm_xlate_errno(rc);
 	}
 
-	rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &hdr);
-	if (rc) {
-		return pldm_xlate_errno(rc);
-	}
-
-	rc = pldm_platform_pdr_hdr_validate(
-		&hdr, PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH,
+	rc = pldm_msgbuf_extract_value_pdr_hdr(
+		buf, &hdr, PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH,
 		pdr_data_length);
 	if (rc) {
-		return rc;
+		return pldm_xlate_errno(rc);
 	}
 
 	memcpy(&pdr_value->hdr, &hdr, sizeof(hdr));
@@ -3074,13 +3050,8 @@
 		return rc;
 	}
 
-	rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &pdr->hdr);
-	if (rc) {
-		return rc;
-	}
-
-	rc = pldm_platform_pdr_hdr_validate(
-		&pdr->hdr, PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH,
+	rc = pldm_msgbuf_extract_value_pdr_hdr(
+		buf, &pdr->hdr, PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH,
 		data_length);
 	if (rc) {
 		return rc;
diff --git a/src/msgbuf/platform.h b/src/msgbuf/platform.h
index 11f0900..897c199 100644
--- a/src/msgbuf/platform.h
+++ b/src/msgbuf/platform.h
@@ -7,17 +7,32 @@
 #include <libpldm/base.h>
 #include <libpldm/platform.h>
 
-static inline int
+LIBPLDM_CC_NONNULL
+LIBPLDM_CC_ALWAYS_INLINE int
 pldm_msgbuf_extract_value_pdr_hdr(struct pldm_msgbuf *ctx,
-				  struct pldm_value_pdr_hdr *hdr)
+				  struct pldm_value_pdr_hdr *hdr, size_t lower,
+				  size_t upper)
 {
+	int rc;
+
 	pldm_msgbuf_extract(ctx, hdr->record_handle);
 	pldm_msgbuf_extract(ctx, hdr->version);
 	pldm_msgbuf_extract(ctx, hdr->type);
 	pldm_msgbuf_extract(ctx, hdr->record_change_num);
-	pldm_msgbuf_extract(ctx, hdr->length);
+	rc = pldm_msgbuf_extract(ctx, hdr->length);
+	if (rc) {
+		return rc;
+	}
 
-	return pldm_msgbuf_validate(ctx);
+	if (hdr->length + sizeof(*ctx) < lower) {
+		return -EOVERFLOW;
+	}
+
+	if (hdr->length > upper) {
+		return -EOVERFLOW;
+	}
+
+	return 0;
 }
 
 LIBPLDM_CC_ALWAYS_INLINE int