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/tests/fuzz/fd-fuzz.cpp b/tests/fuzz/fd-fuzz.cpp
index 9049ab8..258c27e 100644
--- a/tests/fuzz/fd-fuzz.cpp
+++ b/tests/fuzz/fd-fuzz.cpp
@@ -5,6 +5,8 @@
#undef NDEBUG
#endif
+#include "msgbuf.hpp"
+
#include <libpldm/base.h>
#include <libpldm/firmware_fd.h>
#include <libpldm/firmware_update.h>
@@ -17,7 +19,6 @@
#include <vector>
#include "array.h"
-#include "msgbuf.h"
/* Avoid out-of-memory, and
* avoid wasting time on inputs larger than MCTP message limits */
@@ -48,7 +49,7 @@
struct fuzz_ops_ctx
{
- struct pldm_msgbuf* fuzz_ctrl;
+ struct pldm_msgbuf_ro* fuzz_ctrl;
/* Details of in-progress update, for consistency checking */
bool current_update;
@@ -64,7 +65,7 @@
/* Returns true with roughly `percent` chance */
static bool fuzz_chance(struct fuzz_ops_ctx* ctx, uint8_t percent)
{
- uint8_t v;
+ uint8_t v = 0;
assert(percent <= 100);
int rc = pldm_msgbuf_extract_uint8(ctx->fuzz_ctrl, v);
if (rc != 0)
@@ -368,8 +369,8 @@
extern "C" int LLVMFuzzerTestOneInput(uint8_t* input, size_t len)
{
- PLDM_MSGBUF_DEFINE_P(fuzzproto);
- PLDM_MSGBUF_DEFINE_P(fuzzctrl);
+ PLDM_MSGBUF_RO_DEFINE_P(fuzzproto);
+ PLDM_MSGBUF_RO_DEFINE_P(fuzzctrl);
int rc;
/* Split input into two parts. First FUZZCTRL_SIZE (0x400 bytes currently)
@@ -397,7 +398,7 @@
while (true)
{
/* Arbitrary length send buffer */
- uint32_t send_len;
+ uint32_t send_len = 0;
rc = pldm_msgbuf_extract_uint32(fuzzctrl, send_len);
if (rc)
{