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

Code before:
            while (extensions--) {
                uint16_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) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += sizeof(clusterMsgDataPublish) - 8 + ntohl(hdr->data.publish.msg.channel_len) +
                  ntohl(hdr->data.publish.msg.message_len);
    } else if (type == CLUSTERMSG_TYPE_PUBLISH_LIGHT || type == CLUSTERMSG_TYPE_PUBLISHSHARD_LIGHT) {
        clusterMsgLight *hdr_pubsub = (clusterMsgLight *)link->rcvbuf;
        explen = sizeof(clusterMsgLight) - sizeof(union clusterMsgDataLight);
        explen += sizeof(clusterMsgDataPublishMulti);
        uint64_t data_count = ntohu64(hdr_pubsub->data.publish.msg.data_count);
        explen += ((data_count) * (sizeof(clusterMsgDataPublishMessage) - 8));
        clusterMsgDataPublishMessage *msg_data = getPublishMessageFirst(&hdr_pubsub->data.publish.msg);
        while (data_count--) {
            uint32_t msglen = getPublishMsgLength(msg_data);
            explen += msglen;
            msg_data = getPublishMessageNext(msg_data);
        }
    } 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) {
        explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
        explen += sizeof(clusterMsgModule) - 3 + ntohl(hdr->data.module.msg.len);
    } else {
        /* We don't know this type of packet, so we assume it's well formed. */
        explen = totlen;
    }


Review comment (refactor):
Could you move this condition to a macro, seems to be repeated multiple times.


```
#define IS_PUBSUB_LIGHT_MESSAGE (type == CLUSTERMSG_TYPE_PUBLISH_LIGHT || type == CLUSTERMSG_TYPE_PUBLISHSHARD_LIGHT)
```

Code after:
        serverLog(LL_NOTICE,
                  "Packet of type '%s' (%u) does not support light cluster header. Marking packet as invalid.",
                  clusterGetMessageTypeString(type), type);
        return 0;
    }

    if (type < CLUSTERMSG_TYPE_COUNT) server.cluster->stats_bus_messages_received[type]++;

    serverLog(LL_DEBUG, "--- Processing packet of type %s, %lu bytes", clusterGetMessageTypeString(type),
              (unsigned long)totlen);

    /* Perform sanity checks */
    if (totlen < 16) return 0; /* At least signature, version, totlen, count. */
    if (totlen > link->rcvbuf_len) return 0;

    if (ntohs(hdr->ver) != CLUSTER_PROTO_VER) {
        /* Can't handle messages of different versions. */
        return 0;
    }

    if (type == server.cluster_drop_packet_filter) {
        serverLog(LL_WARNING, "Dropping packet that matches debug drop filter");
        return 0;
    }

    uint32_t explen; /* expected length of this packet */

    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_PONG || type == CLUSTERMSG_TYPE_MEET) {
        uint16_t extensions = ntohs(hdr->extensions);
        uint16_t count = ntohs(hdr->count);

        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--) {
                uint16_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;