pdr: pldm_entity_association_pdr_extract(): Early exit on bad arguments

Also identify the conditions in the doxygen commentary in the public
header.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I37945ab559ffdee5648b2d9ea2befc9fc9a24a9c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4549384..9883eb4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -62,6 +62,7 @@
 18. pdr: pldm_find_entity_ref_in_tree(): Exit early on bad arguments
 19. pdr: pldm_entity_association_tree_find(): Early exit on bad arguments
 20. pdr: pldm_entity_association_tree_destroy_root(): Exit early on bad arg
+21. pdr: pldm_entity_association_pdr_extract(): Early exit on bad arguments
 
 ### Deprecated
 
diff --git a/include/libpldm/pdr.h b/include/libpldm/pdr.h
index b05085f..413ef7a 100644
--- a/include/libpldm/pdr.h
+++ b/include/libpldm/pdr.h
@@ -547,6 +547,8 @@
 
 /** @brief Extract entities from entity association PDR
  *
+ *  @pre `*entities == NULL` and `*num_entities == 0` must hold at the time of invocation.
+ *
  *  @param[in] pdr - entity association PDR
  *  @param[in] pdr_len - size of entity association PDR in bytes
  *  @param[out] num_entities - number of entities found, including the container
diff --git a/src/pdr.c b/src/pdr.c
index 8948ea0..7266ecd 100644
--- a/src/pdr.c
+++ b/src/pdr.c
@@ -1252,14 +1252,23 @@
 					 pldm_entity **entities)
 {
 	assert(pdr != NULL);
-	assert(pdr_len >= sizeof(struct pldm_pdr_hdr) +
-				  sizeof(struct pldm_pdr_entity_association));
+	if (!pdr || !num_entities || !entities) {
+		return;
+	}
+#define PDR_MIN_SIZE                                                           \
+	(sizeof(struct pldm_pdr_hdr) +                                         \
+	 sizeof(struct pldm_pdr_entity_association))
+	assert(pdr_len >= PDR_MIN_SIZE);
+	if (pdr_len < PDR_MIN_SIZE) {
+		return;
+	}
+#undef PDR_MIN_SIZE
 
 	struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr;
 	assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION);
 
 	const uint8_t *start = (uint8_t *)pdr;
-	const uint8_t *end =
+	const uint8_t *end __attribute__((unused)) =
 		start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length);
 	start += sizeof(struct pldm_pdr_hdr);
 	struct pldm_pdr_entity_association *entity_association_pdr =