From c0d796d7f80d879b7c67f75984b008c692d708b4 Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Wed, 1 Jul 2020 21:09:25 +0900 Subject: [PATCH] lib/iscsi: Use fixed size char array to parse string of portal address and port Dynamically allocated string is not necessary, and use fixed size char array for simplification instead. Signed-off-by: Shuhei Matsumoto Change-Id: Iada118fbc81f24d0273269f4980bab28bd9c2c23 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/3161 Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Paul Luse Reviewed-by: Jim Harris --- lib/iscsi/portal_grp.c | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/lib/iscsi/portal_grp.c b/lib/iscsi/portal_grp.c index 986562ad7..36065bf74 100644 --- a/lib/iscsi/portal_grp.c +++ b/lib/iscsi/portal_grp.c @@ -216,13 +216,14 @@ iscsi_portal_close(struct spdk_iscsi_portal *p) static int iscsi_parse_portal(const char *portalstring, struct spdk_iscsi_portal **ip) { - char *host = NULL, *port = NULL; - int len, rc = -1; + char host[MAX_PORTAL_ADDR + 1] = {}; + char port[MAX_PORTAL_PORT + 1] = {}; + int len; const char *p; if (portalstring == NULL) { SPDK_ERRLOG("portal error\n"); - goto error_out; + return -EINVAL; } /* IP address */ @@ -231,7 +232,7 @@ iscsi_parse_portal(const char *portalstring, struct spdk_iscsi_portal **ip) p = strchr(portalstring + 1, ']'); if (p == NULL) { SPDK_ERRLOG("portal error\n"); - goto error_out; + return -EINVAL; } p++; } else { @@ -243,29 +244,20 @@ iscsi_parse_portal(const char *portalstring, struct spdk_iscsi_portal **ip) } len = p - portalstring; - host = malloc(len + 1); - if (host == NULL) { - SPDK_ERRLOG("malloc() failed for host\n"); - goto error_out; + if (len > MAX_PORTAL_ADDR) { + return -EINVAL; } memcpy(host, portalstring, len); host[len] = '\0'; /* Port number (IPv4 and IPv6 are the same) */ if (p[0] == '\0') { - port = malloc(PORTNUMSTRLEN); - if (!port) { - SPDK_ERRLOG("malloc() failed for port\n"); - goto error_out; - } - snprintf(port, PORTNUMSTRLEN, "%d", DEFAULT_PORT); + snprintf(port, MAX_PORTAL_PORT, "%d", DEFAULT_PORT); } else { p++; len = strlen(p); - port = malloc(len + 1); - if (port == NULL) { - SPDK_ERRLOG("malloc() failed for port\n"); - goto error_out; + if (len > MAX_PORTAL_PORT) { + return -EINVAL; } memcpy(port, p, len); port[len] = '\0'; @@ -273,15 +265,10 @@ iscsi_parse_portal(const char *portalstring, struct spdk_iscsi_portal **ip) *ip = iscsi_portal_create(host, port); if (!*ip) { - goto error_out; + return -EINVAL; } - rc = 0; -error_out: - free(host); - free(port); - - return rc; + return 0; } struct spdk_iscsi_portal_grp *