From 64f8631ca06273a2d904375e74fdc820a257626a Mon Sep 17 00:00:00 2001 From: Gleb Chesnokov Date: Wed, 21 Oct 2026 15:00:18 +0300 Subject: [PATCH] iscsi-scstd: Limit temporary variables to their processing blocks Move per-address socket variables, parameter-format cursors, netmask fragments, iSNS read results and Base64 words into the loops or branches that use them. Preserve operation order and values carried between iterations. Compute the full initiator name immediately before the kernel access check instead of nesting the helper call inside that check. Keep the name buffer, access-check inputs and call order unchanged. The initiator-name extraction is a readability change found during source review of the nested call, separate from the variableScope report. Cppcheck reports variableScope: The scope of the variable 'sock' can be reduced. --- iscsi-scst/usr/chap.c | 4 +++- iscsi-scst/usr/config.c | 10 ++++++---- iscsi-scst/usr/iscsi_adm.c | 7 +++++-- iscsi-scst/usr/iscsi_scstd.c | 11 ++++++++--- iscsi-scst/usr/isns.c | 4 ++-- iscsi-scst/usr/target.c | 8 ++++---- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/iscsi-scst/usr/chap.c b/iscsi-scst/usr/chap.c index e5cfe670e..8a0e869cf 100644 --- a/iscsi-scst/usr/chap.c +++ b/iscsi-scst/usr/chap.c @@ -189,7 +189,7 @@ static inline void encode_hex_string(u8 *intnum, long length, char *string) /* Base64 encoding, taken from UNH iSCSI "IntegerToBase64String()" */ static void encode_base64_string(u8 *intnum, long length, char *string) { - int count, octets, strptr, delta; + int count, strptr, delta; static const char base64code[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', @@ -208,6 +208,8 @@ static void encode_base64_string(u8 *intnum, long length, char *string) strptr = 0; while ((delta = (length - count)) > 2) { + int octets; + octets = (intnum[count] << 16) | (intnum[count + 1] << 8) | intnum[count + 2]; string[strptr] = base64code[(octets & 0xfc0000) >> 18]; string[strptr + 1] = base64code[(octets & 0x03f000) >> 12]; diff --git a/iscsi-scst/usr/config.c b/iscsi-scst/usr/config.c index d6759ef1f..1c47a77e8 100644 --- a/iscsi-scst/usr/config.c +++ b/iscsi-scst/usr/config.c @@ -605,7 +605,7 @@ typedef union { static int netmask_match_v6(const struct sockaddr_in6 *sa1, const struct sockaddr_in6 *sa2, uint32_t mbit) { - uint16_t mask, a1[8], a2[8]; + uint16_t a1[8], a2[8]; int i; for (i = 0; i < 8; i++) { @@ -618,6 +618,8 @@ static int netmask_match_v6(const struct sockaddr_in6 *sa1, return 0; if (mbit % 16) { + uint16_t mask; + mask = ~((1 << (16 - (mbit % 16))) - 1); if ((mask & a1[mbit / 16]) ^ (mask & a2[mbit / 16])) return 0; @@ -1074,11 +1076,11 @@ static int config_isns_load(const char *config) { char buf[BUFSIZE]; int offset = 0; - char *p, *q; while (config_gets(buf, sizeof(buf), config, &offset)) { - q = buf; - p = config_sep_string(&q); + char *q = buf; + const char *p = config_sep_string(&q); + if ((*p == '\0') || (*p == '#')) continue; if (!strcasecmp(p, ISCSI_ISNS_SERVER_ATTR_NAME)) { diff --git a/iscsi-scst/usr/iscsi_adm.c b/iscsi-scst/usr/iscsi_adm.c index 20ec9f32f..645c118de 100644 --- a/iscsi-scst/usr/iscsi_adm.c +++ b/iscsi-scst/usr/iscsi_adm.c @@ -247,8 +247,8 @@ out_close: static void show_iscsi_params(int type, struct iscsi_param *param) { - int i, nr, len; - char buf[1024], *p; + int i, nr; + char buf[1024]; struct iscsi_key *keys; if (type == key_session) { @@ -260,6 +260,9 @@ static void show_iscsi_params(int type, struct iscsi_param *param) } for (i = 0; i < nr; i++) { + int len; + char *p; + memset(buf, 0, sizeof(buf)); iscsi_strscpy(buf, keys[i].name, sizeof(buf)); len = strlen(buf); diff --git a/iscsi-scst/usr/iscsi_scstd.c b/iscsi-scst/usr/iscsi_scstd.c index 0e9d6aee0..54128470f 100644 --- a/iscsi-scst/usr/iscsi_scstd.c +++ b/iscsi-scst/usr/iscsi_scstd.c @@ -103,7 +103,7 @@ static void create_listen_socket(struct pollfd *array) { struct addrinfo hints, *res, *res0; char servname[64]; - int i, k, sock, opt, rc; + int i, k, opt; memset(servname, 0, sizeof(servname)); snprintf(servname, sizeof(servname), "%d", server_port); @@ -115,6 +115,7 @@ static void create_listen_socket(struct pollfd *array) i = 0; for (k = 0; k < ADDR_MAX; k++) { char *server_address; + int rc; server_address = server_addresses[k]; if (k > 0 && server_address == NULL) @@ -133,6 +134,8 @@ static void create_listen_socket(struct pollfd *array) } for (res = res0; res && i < LISTEN_MAX; res = res->ai_next) { + int sock; + sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sock < 0) { log_error("Unable to create server socket (%s) %d %d %d!", @@ -242,8 +245,7 @@ static void create_iser_listen_socket(struct pollfd *array) { struct addrinfo hints, *res, *res0; char servname[64]; - char *server_address; - int rc, i, k; + int i, k; int iser_fd; struct isert_addr_info info; @@ -271,6 +273,9 @@ static void create_iser_listen_socket(struct pollfd *array) i = 0; for (k = 0; k < ADDR_MAX; k++) { + char *server_address; + int rc; + server_address = server_addresses[k]; if (k > 0 && server_address == NULL) diff --git a/iscsi-scst/usr/isns.c b/iscsi-scst/usr/isns.c index 25212b252..f1fea4523 100644 --- a/iscsi-scst/usr/isns.c +++ b/iscsi-scst/usr/isns.c @@ -680,9 +680,9 @@ out: static int recv_hdr(int fd, struct isns_io *rx, struct isns_hdr *hdr) { - int err; - if (rx->offset < sizeof(*hdr)) { + int err; + err = read(fd, rx->buf + rx->offset, sizeof(*hdr) - rx->offset); if (err < 0) { diff --git a/iscsi-scst/usr/target.c b/iscsi-scst/usr/target.c index bcff10089..71c9c9581 100644 --- a/iscsi-scst/usr/target.c +++ b/iscsi-scst/usr/target.c @@ -164,10 +164,10 @@ int target_portal_allowed(struct target *target, goto out; } - res = kernel_initiator_allowed(target->tid, - iscsi_make_full_initiator_name(target->per_portal_acl, - initiator_name, target_portal, - full_initiator_name, sizeof(full_initiator_name))); + initiator_name = iscsi_make_full_initiator_name(target->per_portal_acl, + initiator_name, target_portal, + full_initiator_name, sizeof(full_initiator_name)); + res = kernel_initiator_allowed(target->tid, initiator_name); if (res < 0) res = 0; /* false */