libpldm: Lift or remove asserts where a subsequent check exists
In the case where an existing public API relies on assert(), move any
asserts from its underlying `*_check()` equivalent up into to the
unchecked function implementation.
Everywhere else, remove asserts where they are unnecessary as the API is
capable of reporting errors.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Iff8ce32b5d5e08ba1244e17d58722a556eca8694
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b162c9..1644d63 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -68,6 +68,7 @@
21. pdr: pldm_entity_association_pdr_extract(): Early exit on bad arguments
22. pdr: pldm_entity_association_pdr_extract(): Assign out params at exit
23. pdr: pldm_entity_get_num_children(): Don't return invalid values
+24. libpldm: Lift or remove asserts where a subsequent check exists
### Deprecated
diff --git a/src/bios_table.c b/src/bios_table.c
index 72bdbfb..1ca984e 100644
--- a/src/bios_table.c
+++ b/src/bios_table.c
@@ -975,7 +975,6 @@
size_t pad_checksum_size = pldm_bios_table_pad_checksum_size(*size);
size_t total_length = *size + pad_checksum_size;
- assert(capacity >= total_length);
if (capacity < total_length) {
return PLDM_ERROR_INVALID_LENGTH;
}
diff --git a/src/fru.c b/src/fru.c
index 9d6f575..56b2ac6 100644
--- a/src/fru.c
+++ b/src/fru.c
@@ -256,7 +256,6 @@
len = sizeof(struct pldm_fru_record_data_format) -
sizeof(struct pldm_fru_record_tlv);
- assert(pos - record_table + len < *record_size);
if (pos - record_table + len >= *record_size) {
return PLDM_ERROR_INVALID_LENGTH;
}
@@ -270,7 +269,6 @@
for (int i = 0; i < record_data_src->num_fru_fields; i++) {
len = sizeof(*tlv) - 1 + tlv->length;
if (tlv->type == ft || ft == 0) {
- assert(pos - record_table + len < *record_size);
if (pos - record_table + len >= *record_size) {
return PLDM_ERROR_INVALID_LENGTH;
}
diff --git a/src/pdr.c b/src/pdr.c
index 7dff0c6..639ddd3 100644
--- a/src/pdr.c
+++ b/src/pdr.c
@@ -39,6 +39,9 @@
uint32_t record_handle, bool is_remote,
uint16_t terminus_handle)
{
+ assert(repo != NULL);
+ assert(data != NULL);
+ assert(size != 0);
int rc = pldm_pdr_add_check(repo, data, size, is_remote,
terminus_handle, &record_handle);
(void)rc;
@@ -51,23 +54,17 @@
bool is_remote, uint16_t terminus_handle,
uint32_t *record_handle)
{
- assert(repo != NULL);
- assert(data != NULL);
- assert(size != 0);
- assert(record_handle != NULL);
if (!repo || !data || !size || !record_handle) {
return -EINVAL;
}
pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record));
- assert(record != NULL);
if (!record) {
return -ENOMEM;
}
if (data) {
record->data = malloc(size);
- assert(record->data != NULL);
if (!record->data) {
free(record);
return -ENOMEM;
@@ -83,7 +80,6 @@
record->record_handle = *record_handle;
} else {
uint32_t curr = repo->last ? repo->last->record_handle : 0;
- assert(curr != UINT32_MAX);
if (curr == UINT32_MAX) {
return -EOVERFLOW;
}
@@ -161,10 +157,6 @@
uint8_t **data, uint32_t *size,
uint32_t *next_record_handle)
{
- assert(repo != NULL);
- assert(data != NULL);
- assert(size != NULL);
- assert(next_record_handle != NULL);
if (!repo || !data || !size || !next_record_handle) {
return NULL;
}
@@ -196,11 +188,6 @@
const pldm_pdr_record *curr_record, uint8_t **data,
uint32_t *size, uint32_t *next_record_handle)
{
- assert(repo != NULL);
- assert(curr_record != NULL);
- assert(data != NULL);
- assert(size != NULL);
- assert(next_record_handle != NULL);
if (!repo || !curr_record || !data || !size || !next_record_handle) {
return NULL;
}
@@ -224,7 +211,6 @@
const pldm_pdr_record *curr_record, uint8_t **data,
uint32_t *size)
{
- assert(repo != NULL);
if (!repo) {
return NULL;
}
@@ -337,10 +323,6 @@
uint16_t *entity_type, uint16_t *entity_instance_num,
uint16_t *container_id)
{
- assert(terminus_handle != NULL);
- assert(entity_type != NULL);
- assert(entity_instance_num != NULL);
- assert(container_id != NULL);
if (!repo || !terminus_handle || !entity_type || !entity_instance_num ||
!container_id) {
return NULL;
@@ -688,7 +670,6 @@
void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
pldm_entity **entities, size_t *size)
{
- assert(tree != NULL);
if (!tree || !entities || !size) {
return;
}
@@ -763,13 +744,10 @@
uint8_t pldm_entity_get_num_children(pldm_entity_node *node,
uint8_t association_type)
{
- assert(node != NULL);
if (!node) {
return 0;
}
- assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
- association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
if (!(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL)) {
return 0;
@@ -791,8 +769,6 @@
LIBPLDM_ABI_STABLE
bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node)
{
- assert(parent != NULL);
- assert(node != NULL);
if (!parent || !node) {
return false;
}
@@ -938,8 +914,6 @@
pldm_pdr *repo, bool is_remote,
uint16_t terminus_handle)
{
- assert(tree != NULL);
- assert(repo != NULL);
if (!tree || !repo) {
return;
}
@@ -1016,7 +990,6 @@
void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree,
pldm_entity entity, pldm_entity_node **node)
{
- assert(tree != NULL);
if (!tree || !node) {
return;
}
@@ -1028,7 +1001,6 @@
void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo,
uint16_t terminus_handle)
{
- assert(repo != NULL);
if (!repo) {
return;
}
@@ -1080,7 +1052,6 @@
LIBPLDM_ABI_STABLE
void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo)
{
- assert(repo != NULL);
if (!repo) {
return;
}
@@ -1156,7 +1127,6 @@
pldm_entity_node **out,
bool is_remote)
{
- assert(out != NULL && *out == NULL);
if (node == NULL) {
return;
}
@@ -1219,7 +1189,6 @@
pldm_entity_association_tree_find(pldm_entity_association_tree *tree,
pldm_entity *entity)
{
- assert(tree != NULL);
if (!tree || !entity) {
return NULL;
}
@@ -1261,7 +1230,6 @@
void pldm_entity_association_tree_destroy_root(
pldm_entity_association_tree *tree)
{
- assert(tree != NULL);
if (!tree) {
return;
}
@@ -1282,21 +1250,18 @@
size_t *num_entities,
pldm_entity **entities)
{
- assert(pdr != NULL);
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);
if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) {
return;
}
@@ -1308,20 +1273,15 @@
struct pldm_pdr_entity_association *entity_association_pdr =
(struct pldm_pdr_entity_association *)start;
size_t l_num_entities = entity_association_pdr->num_children + 1;
- assert(l_num_entities >= 2);
if (l_num_entities < 2) {
return;
}
- assert(start + sizeof(struct pldm_pdr_entity_association) +
- sizeof(pldm_entity) * (l_num_entities - 2) ==
- end);
if (start + sizeof(struct pldm_pdr_entity_association) +
sizeof(pldm_entity) * (l_num_entities - 2) !=
end) {
return;
}
pldm_entity *l_entities = malloc(sizeof(pldm_entity) * l_num_entities);
- assert(l_entities != NULL);
if (!l_entities) {
return;
}