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

Code before:
 * returns 1. If value is nonempty and cursor_ptr points to NULL, just computes
 * the size, increments totlen and returns 1. If value is empty, returns 0. */
static uint32_t
writeSdsPingExtIfNonempty(uint32_t *totlen_ptr, clusterMsgPingExt **cursor_ptr, clusterMsgPingtypes type, sds value) {
    size_t len = sdslen(value);
    if (len == 0) return 0;
    size_t size = getAlignedPingExtSize(len + 1);
    if (*cursor_ptr != NULL) {
        void *ext = preparePingExt(*cursor_ptr, type, size);
        memcpy(ext, value, len);
        *cursor_ptr = getNextPingExt(*cursor_ptr);
    }
    *totlen_ptr += size;
    return 1;
}

static uint32_t
writePortPingExtIfNonzero(uint32_t *totlen_ptr, clusterMsgPingExt **cursor_ptr, clusterMsgPingtypes type, uint16_t value) {
    if (value == 0) return 0;
    size_t size = getAlignedPingExtSize(sizeof(clusterMsgPingExtClientPort));
    if (*cursor_ptr != NULL) {
        void *ext = preparePingExt(*cursor_ptr, type, size);
        memcpy(ext, &value, sizeof(value));
        *cursor_ptr = getNextPingExt(*cursor_ptr);
    }
    *totlen_ptr += size;
    return 1;
}

/* 1. If a NULL hdr is provided, compute the extension size;
 * 2. If a non-NULL hdr is provided, write the ping
 *    extensions at the start of the cursor. This function
 *    will update the cursor to point to the end of the
 *    written extension and will return the amount of bytes
 *    written. */
static uint32_t writePingExtensions(clusterMsg *hdr, int gossipcount) {
    uint16_t extensions = 0;
    uint32_t totlen = 0;
    clusterMsgPingExt *cursor = NULL;
    /* Set the initial extension position */
    if (hdr != NULL) {
        cursor = getInitialPingExt(hdr, gossipcount);
    }

    /* Write simple optional SDS ping extensions. */
    extensions += writeSdsPingExtIfNonempty(&totlen, &cursor, CLUSTERMSG_EXT_TYPE_HOSTNAME, myself->hostname);
    extensions +=
        writeSdsPingExtIfNonempty(&totlen, &cursor, CLUSTERMSG_EXT_TYPE_HUMAN_NODENAME, myself->human_nodename);
    extensions +=
        writeSdsPingExtIfNonempty(&totlen, &cursor, CLUSTERMSG_EXT_TYPE_CLIENT_IPV4, myself->announce_client_ipv4);
    extensions +=

Review comment (suggestion):
We need to handle endianess. All numbers in the cluster bus are stored in network byte order (big endian). Let's convert value before memcpy?

```suggestion
        value = htons(value);
        memcpy(ext, &value, sizeof(value));
```

Code after:
/* If value is nonempty and cursor_ptr points to a non-NULL cursor, writes a
 * ping extension at the cursor, advances the cursor, increments totlen and
 * returns 1. If value is nonempty and cursor_ptr points to NULL, just computes
 * the size, increments totlen and returns 1. If value is empty, returns 0. */
static uint32_t
writeSdsPingExtIfNonempty(uint32_t *totlen_ptr, clusterMsgPingExt **cursor_ptr, clusterMsgPingtypes type, sds value) {
    size_t len = sdslen(value);
    if (len == 0) return 0;
    size_t size = getAlignedPingExtSize(len + 1);
    if (*cursor_ptr != NULL) {
        void *ext = preparePingExt(*cursor_ptr, type, size);
        memcpy(ext, value, len);
        *cursor_ptr = getNextPingExt(*cursor_ptr);
    }
    *totlen_ptr += size;
    return 1;
}

static uint32_t
writePortPingExtIfNonzero(uint32_t *totlen_ptr, clusterMsgPingExt **cursor_ptr, clusterMsgPingtypes type, uint16_t value) {
    if (value == 0) return 0;
    size_t size = getAlignedPingExtSize(sizeof(clusterMsgPingExtClientPort));
    if (*cursor_ptr != NULL) {
        void *ext = preparePingExt(*cursor_ptr, type, size);
        value = htons(value);
        memcpy(ext, &value, sizeof(value));
        *cursor_ptr = getNextPingExt(*cursor_ptr);
    }
    *totlen_ptr += size;
    return 1;
}

/* 1. If a NULL hdr is provided, compute the extension size;
 * 2. If a non-NULL hdr is provided, write the ping
 *    extensions at the start of the cursor. This function
 *    will update the cursor to point to the end of the
 *    written extension and will return the amount of bytes
 *    written. */
static uint32_t writePingExtensions(clusterMsg *hdr, int gossipcount) {
    uint16_t extensions = 0;
    uint32_t totlen = 0;
    clusterMsgPingExt *cursor = NULL;
    /* Set the initial extension position */
    if (hdr != NULL) {
        cursor = getInitialPingExt(hdr, gossipcount);
    }

    /* Write simple optional SDS ping extensions. */
    extensions += writeSdsPingExtIfNonempty(&totlen, &cursor, CLUSTERMSG_EXT_TYPE_HOSTNAME, myself->hostname);
    extensions +=
        writeSdsPingExtIfNonempty(&totlen, &cursor, CLUSTERMSG_EXT_TYPE_HUMAN_NODENAME, myself->human_nodename);