msgbuf: Define a separate msgbuf structure for encode/decode function
Define separate msgbuf structures to avoid casting away const-qualifiers
in the msgbuf constructor function:
* pldm_msgbuf_rw: for encode functions with non const-qualified buffer
* pldm_msgbuf_ro: for decode functions with const-qualified buffer
Further, use _Generic() to keep the API ergonomic while still yielding a
compile error when wrong msgbuf type is passed.
Change-Id: I71dbcb7996e9fb402b49870fce539a939c1497e5
Signed-off-by: John Chung <john.chung@arm.com>
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/src/dsp/pdr.c b/src/dsp/pdr.c
index f8873b1..fd36b56 100644
--- a/src/dsp/pdr.c
+++ b/src/dsp/pdr.c
@@ -6,10 +6,11 @@
#include <assert.h>
#include <endian.h>
+#include <errno.h>
+#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
-#include <errno.h>
#define PDR_ENTITY_ASSOCIATION_MIN_SIZE \
(sizeof(struct pldm_pdr_hdr) + \
@@ -407,7 +408,7 @@
static int decode_pldm_state_sensor_pdr(uint8_t *data, uint32_t size,
struct pldm_state_sensor_pdr *pdr)
{
- PLDM_MSGBUF_DEFINE_P(buf);
+ PLDM_MSGBUF_RO_DEFINE_P(buf);
int rc = 0;
rc = pldm_msgbuf_init_errno(buf, sizeof(struct pldm_state_sensor_pdr),
(uint8_t *)data, size);
@@ -534,7 +535,7 @@
static int decode_pldm_state_effecter_pdr(uint8_t *data, uint32_t size,
struct pldm_state_effecter_pdr *pdr)
{
- PLDM_MSGBUF_DEFINE_P(buf);
+ PLDM_MSGBUF_RO_DEFINE_P(buf);
int rc = 0;
rc = pldm_msgbuf_init_errno(buf, sizeof(struct pldm_state_effecter_pdr),
(uint8_t *)data, size);
@@ -619,8 +620,8 @@
record = repo->first;
while (record != NULL) {
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf *buf = &_buf;
+ struct pldm_msgbuf_ro _buf;
+ struct pldm_msgbuf_ro *buf = &_buf;
rc = pldm_msgbuf_init_errno(buf, sizeof(struct pldm_pdr_hdr),
record->data, record->size);
@@ -1701,8 +1702,8 @@
int rc = 0;
uint16_t header_length = 0;
uint8_t num_children = 0;
- PLDM_MSGBUF_DEFINE_P(src);
- PLDM_MSGBUF_DEFINE_P(dst);
+ PLDM_MSGBUF_RO_DEFINE_P(src);
+ PLDM_MSGBUF_RW_DEFINE_P(dst);
pldm_pdr_find_record_by_handle(&record, &prev, pdr_record_handle);
@@ -1851,9 +1852,9 @@
uint16_t new_pdr_size;
uint16_t container_id = 0;
void *container_id_addr;
- PLDM_MSGBUF_DEFINE_P(dst);
- PLDM_MSGBUF_DEFINE_P(src_p);
- PLDM_MSGBUF_DEFINE_P(src_c);
+ PLDM_MSGBUF_RW_DEFINE_P(dst);
+ PLDM_MSGBUF_RO_DEFINE_P(src_p);
+ PLDM_MSGBUF_RO_DEFINE_P(src_c);
int rc = 0;
pldm_pdr_record *prev = repo->first;
@@ -1998,7 +1999,7 @@
pldm_pdr_record *record = repo->first;
while (record != NULL) {
- PLDM_MSGBUF_DEFINE_P(dst);
+ PLDM_MSGBUF_RO_DEFINE_P(dst);
rc = pldm_msgbuf_init_errno(dst,
PDR_ENTITY_ASSOCIATION_MIN_SIZE,
@@ -2057,8 +2058,8 @@
{
uint16_t header_length = 0;
uint8_t num_children = 0;
- PLDM_MSGBUF_DEFINE_P(src);
- PLDM_MSGBUF_DEFINE_P(dst);
+ PLDM_MSGBUF_RO_DEFINE_P(src);
+ PLDM_MSGBUF_RW_DEFINE_P(dst);
int rc;
pldm_pdr_record *record;
pldm_pdr_record *prev;
@@ -2241,7 +2242,7 @@
uint16_t record_fru_rsi = 0;
uint8_t *skip_data = NULL;
uint8_t skip_data_size = 0;
- PLDM_MSGBUF_DEFINE_P(dst);
+ PLDM_MSGBUF_RO_DEFINE_P(dst);
int rc = 0;
rc = pldm_msgbuf_init_errno(dst, PDR_FRU_RECORD_SET_MIN_SIZE,
@@ -2250,7 +2251,8 @@
return rc;
}
skip_data_size = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t);
- pldm_msgbuf_span_required(dst, skip_data_size, (void **)&skip_data);
+ pldm_msgbuf_span_required(dst, skip_data_size,
+ (const void **)&skip_data);
pldm_msgbuf_extract(dst, record_fru_rsi);
rc = pldm_msgbuf_complete(dst);
@@ -2315,7 +2317,7 @@
record = repo->first;
while (record != NULL) {
- PLDM_MSGBUF_DEFINE_P(buf);
+ PLDM_MSGBUF_RO_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(buf, PDR_FRU_RECORD_SET_MIN_SIZE,
record->data, record->size);