msgbuf: Allow pldm_msgbuf_span_required to accept NULL

Allow pldm_msgbuf_span_required to accept NULL as an argument
so we can use this API to skip past data in the msg buffer which
is not required and extract only the relevant data.

Change-Id: I08d233b8efe415732fb7c01c00a9925f04666fe2
Signed-off-by: Varsha Kaverappa <vkaverap@in.ibm.com>
diff --git a/src/msgbuf.h b/src/msgbuf.h
index 38c7055..6f50b05 100644
--- a/src/msgbuf.h
+++ b/src/msgbuf.h
@@ -1056,12 +1056,12 @@
 		char: pldm_msgbuf_insert_array_char)(dst, count, src,          \
 						     src_count)
 
-LIBPLDM_CC_NONNULL
+LIBPLDM_CC_NONNULL_ARGS(1)
 LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_span_required(struct pldm_msgbuf *ctx,
 						       size_t required,
 						       void **cursor)
 {
-	if (!ctx->cursor || *cursor) {
+	if (!ctx->cursor || (cursor && *cursor)) {
 		return pldm_msgbuf_status(ctx, EINVAL);
 	}
 
@@ -1080,7 +1080,9 @@
 		return pldm_msgbuf_status(ctx, EOVERFLOW);
 	}
 
-	*cursor = ctx->cursor;
+	if (cursor) {
+		*cursor = ctx->cursor;
+	}
 	ctx->cursor += required;
 
 	return 0;
diff --git a/tests/msgbuf.cpp b/tests/msgbuf.cpp
index 111200a..7ddd2fb 100644
--- a/tests/msgbuf.cpp
+++ b/tests/msgbuf.cpp
@@ -873,6 +873,7 @@
     struct pldm_msgbuf* ctx = &_ctx;
     uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
     uint8_t buf[6] = {0};
+    const size_t required = 4;
     uint16_t testVal;
     [[maybe_unused]] uint8_t* retBuff = NULL;
 
@@ -887,6 +888,8 @@
     ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
               PLDM_SUCCESS);
     EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
+    EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL),
+              PLDM_SUCCESS);
 
     EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
     EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);