From 12d522515fa412b5b5b4c7023e812448a0a84f8f Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Wed, 23 Feb 2022 03:41:06 +0000 Subject: [PATCH] nvme: simplify spdk_nvme_transport_id_populate_trstring Note that this also works around a false positive in gcc-11 of type -Wstringop-overread. Fixes issue #2391. Signed-off-by: Jim Harris Change-Id: Ib5301b9ef9fa3ead2a1a2318655533a8cfba03fe Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11709 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Dong Yi Reviewed-by: Aleksey Marchuk Reviewed-by: Tomasz Zawadzki --- lib/nvme/nvme.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index 8b0e93683..bdc3db23e 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -1068,27 +1068,24 @@ spdk_nvme_trid_populate_transport(struct spdk_nvme_transport_id *trid, int spdk_nvme_transport_id_populate_trstring(struct spdk_nvme_transport_id *trid, const char *trstring) { - int len, i, rc; + int i = 0; - if (trstring == NULL) { - return -EINVAL; - } - - len = strnlen(trstring, SPDK_NVMF_TRSTRING_MAX_LEN); - if (len == SPDK_NVMF_TRSTRING_MAX_LEN) { - return -EINVAL; - } - - rc = snprintf(trid->trstring, SPDK_NVMF_TRSTRING_MAX_LEN, "%s", trstring); - if (rc < 0) { - return rc; - } + /* Note: gcc-11 has some false positive -Wstringop-overread warnings with LTO builds if we + * use strnlen here. So do the trstring copy manually instead. See GitHub issue #2391. + */ /* cast official trstring to uppercase version of input. */ - for (i = 0; i < len; i++) { - trid->trstring[i] = toupper(trid->trstring[i]); + while (i < SPDK_NVMF_TRSTRING_MAX_LEN && trstring[i] != 0) { + trid->trstring[i] = toupper(trstring[i]); + i++; + } + + if (trstring[i] != 0) { + return -EINVAL; + } else { + trid->trstring[i] = 0; + return 0; } - return 0; } int