msgbuf: Rework error handling to improve soundness

Design the implementation to uphold the invariant that a non-negative
remaining value implies the cursor pointer is valid, and that under
other conditions error values must be observed by the msgbuf user. The
former is tested with assertions in the implementation. The latter is
enforced by construction.

With this change, all msgbuf instances for which
pldm_msgbuf_init_errno() succeeds must be either completed or discarded
by calls to the pldm_msgbuf_complete*() or pldm_msgbuf_discard() APIs
respectively.

We then build on the properties that:

- pldm_msgbuf_init_errno() is marked with the warn_unused_result
  function attribute

- pldm_msgbuf_init_errno() returns errors for invalid buffer
  configurations

- The complete and discard APIs are marked with the warn_unused_result
  function attribute

- The complete APIs test for negative remaining values and return an
  error if encountered.

- The discard API propagates the provided error code

Together these provide the foundation to ensure that buffer access
errors are (eventually) detected.

A msgbuf object is always in one of the uninitialized, valid, invalid,
or completed states. The states are defined as follows:

- Uninitialized: Undefined values for remaining and cursor

- Valid: cursor points to a valid object, remaining is both non-negative
         and describes a range contained within the object pointed to
         by cursor

- Invalid: The value of remaining is negative. The value of cursor is
           unspecified.

- Completed: the value of remaining is INTMAX_MIN and cursor is NULL

msgbuf instances must always be in the completed state by the time
their storage is reclaimed. To enforce this, PLDM_MSGBUF_DEFINE_P()
is introduced both to simplify definition of related variables, and
to exploit the compiler's 'cleanup' attribute. The cleanup function
associated with the msgbuf object asserts that the referenced object is
in the completed state.

From there, update the implementations of the msgbuf APIs such that
exceeding implementation type limits forces the msgbuf object to the
invalid state (in addition to returning an error value) to relieve the
caller from testing the result of all API invocations.

Change-Id: I4d78ddc5f567d4148f2f6d8f3e7570e97c316bbb
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/src/oem/meta/file_io.c b/src/oem/meta/file_io.c
index cd125ef..d3e7a6d 100644
--- a/src/oem/meta/file_io.c
+++ b/src/oem/meta/file_io.c
@@ -21,8 +21,7 @@
 	const struct pldm_msg *msg, size_t payload_length,
 	struct pldm_oem_meta_file_io_write_req *req, size_t req_length)
 {
-	struct pldm_msgbuf _buf;
-	struct pldm_msgbuf *buf = &_buf;
+	PLDM_MSGBUF_DEFINE_P(buf);
 	int rc;
 
 	if (msg == NULL || req == NULL) {
@@ -43,13 +42,13 @@
 	pldm_msgbuf_extract(buf, req->handle);
 	rc = pldm_msgbuf_extract(buf, req->length);
 	if (rc) {
-		return rc;
+		return pldm_msgbuf_discard(buf, rc);
 	}
 
 	rc = pldm_msgbuf_extract_array(buf, req->length, req->data,
 				       req_length - sizeof(*req));
 	if (rc) {
-		return rc;
+		return pldm_msgbuf_discard(buf, rc);
 	}
 
 	return pldm_msgbuf_complete_consumed(buf);
@@ -99,8 +98,7 @@
 				     size_t payload_length,
 				     struct pldm_oem_meta_file_io_read_req *req)
 {
-	struct pldm_msgbuf _buf;
-	struct pldm_msgbuf *buf = &_buf;
+	PLDM_MSGBUF_DEFINE_P(buf);
 
 	if (msg == NULL || req == NULL) {
 		return -EINVAL;
@@ -120,18 +118,18 @@
 	pldm_msgbuf_extract(buf, req->handle);
 	rc = pldm_msgbuf_extract(buf, req->option);
 	if (rc) {
-		return rc;
+		return pldm_msgbuf_discard(buf, rc);
 	}
 
 	rc = pldm_msgbuf_extract(buf, req->length);
 	if (rc) {
-		return rc;
+		return pldm_msgbuf_discard(buf, rc);
 	}
 
 	switch (req->option) {
 	case PLDM_OEM_META_FILE_IO_READ_ATTR:
 		if (req->length != 0) {
-			return -EPROTO;
+			return pldm_msgbuf_discard(buf, -EPROTO);
 		}
 		break;
 	case PLDM_OEM_META_FILE_IO_READ_DATA:
@@ -139,7 +137,7 @@
 		pldm_msgbuf_extract(buf, req->info.data.offset);
 		break;
 	default:
-		return -EPROTO;
+		return pldm_msgbuf_discard(buf, -EPROTO);
 	}
 
 	return pldm_msgbuf_complete_consumed(buf);
@@ -158,8 +156,7 @@
 	size_t resp_len, struct pldm_msg *responseMsg, size_t payload_length)
 {
 	int rc;
-	struct pldm_msgbuf _buf;
-	struct pldm_msgbuf *buf = &_buf;
+	PLDM_MSGBUF_DEFINE_P(buf);
 	struct pldm_header_info header = { 0 };
 
 	if (resp == NULL || responseMsg == NULL) {
@@ -207,11 +204,11 @@
 						    resp->data,
 						    resp_len - sizeof(*resp));
 		if (rc) {
-			return rc;
+			return pldm_msgbuf_discard(buf, rc);
 		}
 		break;
 	default:
-		return -EPROTO;
+		return pldm_msgbuf_discard(buf, -EPROTO);
 	}
 
 	return pldm_msgbuf_complete(buf);