From 81fc0c314386378b9b01e7f7665c506ba574529a Mon Sep 17 00:00:00 2001 From: Caleb Sander Date: Fri, 13 Oct 2023 19:28:09 -0600 Subject: [PATCH] test: don't pass NULL to memcmp() or memset() According to the C standard, it is undefined behavior to pass NULL pointers to standard library functions. This includes the mem*() family of functions, even they are passed a length of 0. So avoid calling these functions when the length is 0. Signed-off-by: Caleb Sander --- test/ioctl/mock.c | 10 ++++++---- test/mi.c | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/test/ioctl/mock.c b/test/ioctl/mock.c index 5d2ac94e..a97a357e 100644 --- a/test/ioctl/mock.c +++ b/test/ioctl/mock.c @@ -75,10 +75,12 @@ void end_mock_cmds(void) check((cmd)->metadata_len == (mock_cmd)->metadata_len, \ "got metadata_len %" PRIu32 ", expected %" PRIu32, \ (cmd)->metadata_len, (mock_cmd)->metadata_len); \ - cmp((void const *)(uintptr_t)(cmd)->metadata, \ - (mock_cmd)->metadata, \ - (cmd)->metadata_len, \ - "incorrect metadata"); \ + if ((cmd)->metadata_len) { \ + cmp((void const *)(uintptr_t)(cmd)->metadata, \ + (mock_cmd)->metadata, \ + (cmd)->metadata_len, \ + "incorrect metadata"); \ + } \ __u32 data_len = (cmd)->data_len; \ check(data_len == (mock_cmd)->data_len, \ "got data_len %" PRIu32 ", expected %" PRIu32, \ diff --git a/test/mi.c b/test/mi.c index 1f95db0d..91b827a1 100644 --- a/test/mi.c +++ b/test/mi.c @@ -44,7 +44,8 @@ static int test_transport_submit(struct nvme_mi_ep *ep, /* start from a minimal response: zeroed data, nmp to match request */ memset(resp->hdr, 0, resp->hdr_len); - memset(resp->data, 0, resp->data_len); + if (resp->data_len) + memset(resp->data, 0, resp->data_len); resp->hdr->type = NVME_MI_MSGTYPE_NVME; resp->hdr->nmp = req->hdr->nmp | (NVME_MI_ROR_RSP << 7); -- 2.50.1