diff --git a/src/cluster.c b/src/cluster.c index caa1e5798b..a2683d33ac 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -778,6 +778,33 @@ int isValidAuxString(char *s, unsigned int length) { return 1; } +int verifyPortNumber(client *c, long long *port, long long *cport) { + if (getLongLongFromObject(c->argv[3], port) != C_OK) { + addReplyErrorFormat(c, "Invalid base port specified: %s", (char *)c->argv[3]->ptr); + return C_ERR; + } + if (*port <= 0 || *port > 65535) { + addReplyErrorFormat(c, "Port number is out of range"); + return C_ERR; + } + + if (c->argc == 5) { + if (getLongLongFromObject(c->argv[4], cport) != C_OK) { + addReplyErrorFormat(c, "Invalid bus port specified: %s", (char *)c->argv[4]->ptr); + return C_ERR; + } + } else { + *cport = *port + CLUSTER_PORT_INCR; + } + + if (*cport <= 0 || *cport > 65535) { + addReplyErrorFormat(c, "Cport number is out of range"); + return C_ERR; + } + + return C_OK; +} + void clusterCommandMyId(client *c) { char *name = clusterNodeGetName(getMyClusterNode()); if (name) { diff --git a/src/cluster.h b/src/cluster.h index 142f2d70b3..fdc7099509 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -134,4 +134,5 @@ int isNodeAvailable(clusterNode *node); long long getNodeReplicationOffset(clusterNode *node); sds aggregateClientOutputBuffer(client *c); void resetClusterStats(void); +int verifyPortNumber(client *c, long long *port, long long *cport); #endif /* __CLUSTER_H */ diff --git a/src/cluster_legacy.c b/src/cluster_legacy.c index 1facd9a94b..32d9ecc7fc 100644 --- a/src/cluster_legacy.c +++ b/src/cluster_legacy.c @@ -6880,27 +6880,7 @@ int clusterCommandSpecial(client *c) { /* CLUSTER MEET [cport] */ long long port, cport; - if (getLongLongFromObject(c->argv[3], &port) != C_OK) { - addReplyErrorFormat(c, "Invalid base port specified: %s", (char *)c->argv[3]->ptr); - return 1; - } - - if (port <= 0 || port > 65535) { - addReplyErrorFormat(c, "Port number is out of range"); - return 1; - } - - if (c->argc == 5) { - if (getLongLongFromObject(c->argv[4], &cport) != C_OK) { - addReplyErrorFormat(c, "Invalid bus port specified: %s", (char *)c->argv[4]->ptr); - return 1; - } - } else { - cport = port + CLUSTER_PORT_INCR; - } - - if (cport <= 0 || cport > 65535) { - addReplyErrorFormat(c, "Cport number is out of range"); + if (verifyPortNumber(c, &port, &cport) == C_ERR) { return 1; }