From: Caleb Sander Date: Sat, 12 Aug 2023 19:55:12 +0000 (-0600) Subject: test: pass a large enough buffer to nvme_identify_ns_descs() X-Git-Tag: v1.6~51 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=b21ad1f350db19982f1b53c0150fd70b31a208cd;p=users%2Fsagi%2Flibnvme.git test: pass a large enough buffer to nvme_identify_ns_descs() nvme_identify_ns_descs() takes a struct nvme_ns_id_desc * parameter, but passes it as the data to nvme_identify(), which sets data_len = 4K. But struct nvme_ns_id_desc only represents the start of a single Namespace Identification Descriptor, so it is less than 4 KB. So it needs to be explicitly allocated with at least 4 KB. Allocate a 4 KB buffer in test.c to avoid a stack buffer overflow. Signed-off-by: Caleb Sander --- diff --git a/test/test.c b/test/test.c index e41fc9d5..abaf74e3 100644 --- a/test/test.c +++ b/test/test.c @@ -16,6 +16,7 @@ * somewhere. */ #include +#include #include #include #include @@ -274,7 +275,7 @@ static int test_namespace(nvme_ns_t n) { int ret, nsid = nvme_ns_get_nsid(n), fd = nvme_ns_get_fd(n); struct nvme_id_ns ns = { 0 }, allocated = { 0 }; - struct nvme_ns_id_desc descs = { 0 }; + struct nvme_ns_id_desc *descs; __u32 result = 0; __u8 flbas; @@ -292,11 +293,16 @@ static int test_namespace(nvme_ns_t n) printf(" Identify allocated ns\n"); else printf(" ERROR: Identify allocated ns:%x\n", ret); - ret = nvme_identify_ns_descs(fd, nsid, &descs); + descs = malloc(NVME_IDENTIFY_DATA_SIZE); + if (!descs) + return -1; + + ret = nvme_identify_ns_descs(fd, nsid, descs); if (!ret) printf(" Identify NS Descriptors\n"); else printf(" ERROR: Identify NS Descriptors:%x\n", ret); + free(descs); ret = nvme_get_features_write_protect(fd, nsid, NVME_GET_FEATURES_SEL_CURRENT, &result); if (!ret)