Repository: valkey-io/valkey
File: src/cluster_legacy.c (C)

Code before:
    clusterMsgPingExt *ext = getInitialPingExt(hdr, ntohs(hdr->count));
    while (extensions--) {
        uint16_t type = ntohs(ext->type);
        if (type == CLUSTERMSG_EXT_TYPE_HOSTNAME) {
            clusterMsgPingExtHostname *hostname_ext = (clusterMsgPingExtHostname *)&(ext->ext[0].hostname);
            ext_hostname = hostname_ext->hostname;
        } else if (type == CLUSTERMSG_EXT_TYPE_HUMAN_NODENAME) {
            clusterMsgPingExtHumanNodename *humannodename_ext =
                (clusterMsgPingExtHumanNodename *)&(ext->ext[0].human_nodename);
            ext_humannodename = humannodename_ext->human_nodename;
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_IPV4) {
            clusterMsgPingExtClientIpV4 *clientipv4_ext =
                (clusterMsgPingExtClientIpV4 *)&(ext->ext[0].announce_client_ipv4);
            ext_clientipv4 = clientipv4_ext->announce_client_ipv4;
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_IPV6) {
            clusterMsgPingExtClientIpV6 *clientipv6_ext =
                (clusterMsgPingExtClientIpV6 *)&(ext->ext[0].announce_client_ipv6);
            ext_clientipv6 = clientipv6_ext->announce_client_ipv6;
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_PORT) {
            clusterMsgPingExtClientPort *clientport_ext =
                (clusterMsgPingExtClientPort *)&(ext->ext[0].announce_client_port);
            ext_clientport = clientport_ext->announce_client_port;
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_TLS_PORT) {
            clusterMsgPingExtClientTlsPort *clienttlsport_ext =
                (clusterMsgPingExtClientTlsPort *)&(ext->ext[0].announce_client_tls_port);
            ext_clienttlsport = clienttlsport_ext->announce_client_tls_port;
        } else if (type == CLUSTERMSG_EXT_TYPE_FORGOTTEN_NODE) {
            clusterMsgPingExtForgottenNode *forgotten_node_ext = &(ext->ext[0].forgotten_node);
            clusterNode *n = clusterLookupNode(forgotten_node_ext->name, CLUSTER_NAMELEN);
            if (n && n != myself && !(nodeIsReplica(myself) && myself->replicaof == n)) {
                sds id = sdsnewlen(forgotten_node_ext->name, CLUSTER_NAMELEN);
                dictEntry *de = dictAddOrFind(server.cluster->nodes_black_list, id);
                if (dictGetKey(de) != id) {
                    /* The dict did not take ownership of the id string, so we need to free it. */
                    sdsfree(id);
                }
                uint64_t expire = server.unixtime + ntohu64(forgotten_node_ext->ttl);
                dictSetUnsignedIntegerVal(de, expire);
                clusterDelNode(n);
                clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_SAVE_CONFIG);
            }
        } else if (type == CLUSTERMSG_EXT_TYPE_SHARDID) {
            clusterMsgPingExtShardId *shardid_ext = (clusterMsgPingExtShardId *)&(ext->ext[0].shard_id);
            ext_shardid = shardid_ext->shard_id;
        } else {
            /* Unknown type, we will ignore it but log what happened. */
            serverLog(LL_WARNING, "Received unknown extension type %d", type);
        }

        /* We know this will be valid since we validated it ahead of time */
        ext = getNextPingExt(ext);

Review comment (suggestion):
```suggestion
            ext_clienttlsport = ntohs(clienttlsport_ext->announce_client_tls_port);
```

Code after:
    char *ext_shardid = NULL;
    uint16_t extensions = ntohs(hdr->extensions);
    /* Loop through all the extensions and process them */
    clusterMsgPingExt *ext = getInitialPingExt(hdr, ntohs(hdr->count));
    while (extensions--) {
        uint16_t type = ntohs(ext->type);
        if (type == CLUSTERMSG_EXT_TYPE_HOSTNAME) {
            clusterMsgPingExtHostname *hostname_ext = (clusterMsgPingExtHostname *)&(ext->ext[0].hostname);
            ext_hostname = hostname_ext->hostname;
        } else if (type == CLUSTERMSG_EXT_TYPE_HUMAN_NODENAME) {
            clusterMsgPingExtHumanNodename *humannodename_ext =
                (clusterMsgPingExtHumanNodename *)&(ext->ext[0].human_nodename);
            ext_humannodename = humannodename_ext->human_nodename;
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_IPV4) {
            clusterMsgPingExtClientIpV4 *clientipv4_ext =
                (clusterMsgPingExtClientIpV4 *)&(ext->ext[0].announce_client_ipv4);
            ext_clientipv4 = clientipv4_ext->announce_client_ipv4;
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_IPV6) {
            clusterMsgPingExtClientIpV6 *clientipv6_ext =
                (clusterMsgPingExtClientIpV6 *)&(ext->ext[0].announce_client_ipv6);
            ext_clientipv6 = clientipv6_ext->announce_client_ipv6;
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_PORT) {
            clusterMsgPingExtClientPort *clientport_ext =
                (clusterMsgPingExtClientPort *)&(ext->ext[0].announce_client_port);
            ext_clientport = ntohs(clientport_ext->announce_client_port);
        } else if (type == CLUSTERMSG_EXT_TYPE_CLIENT_TLS_PORT) {
            clusterMsgPingExtClientTlsPort *clienttlsport_ext =
                (clusterMsgPingExtClientTlsPort *)&(ext->ext[0].announce_client_tls_port);
            ext_clienttlsport = ntohs(clienttlsport_ext->announce_client_tls_port);
        } else if (type == CLUSTERMSG_EXT_TYPE_FORGOTTEN_NODE) {
            clusterMsgPingExtForgottenNode *forgotten_node_ext = &(ext->ext[0].forgotten_node);
            clusterNode *n = clusterLookupNode(forgotten_node_ext->name, CLUSTER_NAMELEN);
            if (n && n != myself && !(nodeIsReplica(myself) && myself->replicaof == n)) {
                sds id = sdsnewlen(forgotten_node_ext->name, CLUSTER_NAMELEN);
                dictEntry *de = dictAddOrFind(server.cluster->nodes_black_list, id);
                if (dictGetKey(de) != id) {
                    /* The dict did not take ownership of the id string, so we need to free it. */
                    sdsfree(id);
                }
                uint64_t expire = server.unixtime + ntohu64(forgotten_node_ext->ttl);
                dictSetUnsignedIntegerVal(de, expire);
                clusterDelNode(n);
                clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_SAVE_CONFIG);
            }
        } else if (type == CLUSTERMSG_EXT_TYPE_SHARDID) {
            clusterMsgPingExtShardId *shardid_ext = (clusterMsgPingExtShardId *)&(ext->ext[0].shard_id);
            ext_shardid = shardid_ext->shard_id;
        } else {
            /* Unknown type, we will ignore it but log what happened. */
            serverLog(LL_WARNING, "Received unknown extension type %d", type);
        }