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

Code before:

        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += (sizeof(clusterMsgDataGossip) * count);

        /* If there is extension data, which doesn't have a fixed length,
         * loop through them and validate the length of it now. */
        if (hdr->mflags[0] & CLUSTERMSG_FLAG0_EXT_DATA) {
            clusterMsgPingExt *ext = getInitialPingExt(hdr, count);
            while (extensions--) {
                uint32_t extlen = getPingExtLength(ext);
                if (extlen % 8 != 0) {
                    serverLog(LL_WARNING, "Received a %s packet without proper padding (%d bytes)",
                              clusterGetMessageTypeString(type), (int)extlen);
                    return 0;
                }
                if ((totlen - explen) < extlen) {
                    serverLog(LL_WARNING,
                              "Received invalid %s packet with extension data that exceeds "
                              "total packet length (%lld)",
                              clusterGetMessageTypeString(type), (unsigned long long)totlen);
                    return 0;
                }
                explen += extlen;
                ext = getNextPingExt(ext);
            }
            link->support_extension = 1;
        }
    } else if (type == CLUSTERMSG_TYPE_FAIL) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += sizeof(clusterMsgDataFail);
    } else if (type == CLUSTERMSG_TYPE_PUBLISH || type == CLUSTERMSG_TYPE_PUBLISHSHARD) {
        clusterMsgDataPublish *publish_data;
        if (is_light) {
            clusterMsgLight *hdr_light = (clusterMsgLight *)link->rcvbuf;
            publish_data = &hdr_light->data.publish.msg;
            explen = sizeof(clusterMsgLight);
        } else {
            publish_data = &hdr->data.publish.msg;
            explen = sizeof(clusterMsg);
        }
        explen -= sizeof(union clusterMsgData);
        explen +=
            sizeof(clusterMsgDataPublish) - 8 + ntohl(publish_data->channel_len) + ntohl(publish_data->message_len);
    } else if (type == CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST || type == CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK ||
               type == CLUSTERMSG_TYPE_MFSTART) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
    } else if (type == CLUSTERMSG_TYPE_UPDATE) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += sizeof(clusterMsgDataUpdate);
    } else if (type == CLUSTERMSG_TYPE_MODULE) {
        if (is_light) {

Review comment (suggestion):
Nit:

Btw if we move the extensions processing ahead of the core message processing. I would prefer this check to be moved to `clusterProcessPacket`, this feels like a bit of bypassing.

Code after:

        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += (sizeof(clusterMsgDataGossip) * count);

        /* If there is extension data, which doesn't have a fixed length,
         * loop through them and validate the length of it now. */
        if (hdr->mflags[0] & CLUSTERMSG_FLAG0_EXT_DATA) {
            clusterMsgPingExt *ext = getInitialPingExt(hdr, count);
            while (extensions--) {
                uint32_t extlen = getPingExtLength(ext);
                if (extlen % 8 != 0) {
                    serverLog(LL_WARNING, "Received a %s packet without proper padding (%d bytes)",
                              clusterGetMessageTypeString(type), (int)extlen);
                    return 0;
                }
                if ((totlen - explen) < extlen) {
                    serverLog(LL_WARNING,
                              "Received invalid %s packet with extension data that exceeds "
                              "total packet length (%lld)",
                              clusterGetMessageTypeString(type), (unsigned long long)totlen);
                    return 0;
                }
                explen += extlen;
                ext = getNextPingExt(ext);
            }
        }
    } else if (type == CLUSTERMSG_TYPE_FAIL) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += sizeof(clusterMsgDataFail);
    } else if (type == CLUSTERMSG_TYPE_PUBLISH || type == CLUSTERMSG_TYPE_PUBLISHSHARD) {
        clusterMsgDataPublish *publish_data;
        if (is_light) {
            clusterMsgLight *hdr_light = (clusterMsgLight *)link->rcvbuf;
            publish_data = &hdr_light->data.publish.msg;
            explen = sizeof(clusterMsgLight);
        } else {
            publish_data = &hdr->data.publish.msg;
            explen = sizeof(clusterMsg);
        }
        explen -= sizeof(union clusterMsgData);
        explen +=
            sizeof(clusterMsgDataPublish) - 8 + ntohl(publish_data->channel_len) + ntohl(publish_data->message_len);
    } else if (type == CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST || type == CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK ||
               type == CLUSTERMSG_TYPE_MFSTART) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
    } else if (type == CLUSTERMSG_TYPE_UPDATE) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += sizeof(clusterMsgDataUpdate);
    } else if (type == CLUSTERMSG_TYPE_MODULE) {
        if (is_light) {
            clusterMsgLight *hdr_light = (clusterMsgLight *)link->rcvbuf;