Fix crash adding duplicate replica. Just send an error instead

Former-commit-id: 97b65ce19c09188acd0d662aad8d774a55531437
This commit is contained in:
John Sully 2019-06-02 15:50:50 -04:00
parent 8c6075b21e
commit 88b5a4e25f

View File

@ -2240,7 +2240,8 @@ struct redisMaster *replicationAddMaster(char *ip, int port) {
while ((ln = listNext(&li)))
{
redisMaster *miCheck = (redisMaster*)listNodeValue(ln);
serverAssert(strcasecmp(miCheck->masterhost, ip) || miCheck->masterport != port);
if (strcasecmp(miCheck->masterhost, ip)==0 && miCheck->masterport == port)
return nullptr;
}
// Pre-req satisfied, lets continue
@ -2380,26 +2381,18 @@ void replicaofCommand(client *c) {
if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != C_OK))
return;
/* Check if we are already attached to the specified slave */
listIter li;
listNode *ln;
listRewind(g_pserver->masters, &li);
while ((ln = listNext(&li)))
{
redisMaster *mi = (redisMaster*)listNodeValue(ln);
if (!strcasecmp(mi->masterhost,(const char*)ptrFromObj(c->argv[1]))
&& mi->masterport == port) {
serverLog(LL_NOTICE,"REPLICAOF would result into synchronization "
"with the master we are already connected "
"with. No operation performed.");
addReplySds(c,sdsnew("+OK Already connected to specified "
"master\r\n"));
return;
}
}
/* There was no previous master or the user specified a different one,
* we can continue. */
redisMaster *miNew = replicationAddMaster((char*)ptrFromObj(c->argv[1]), port);
if (miNew == nullptr)
{
// We have a duplicate
serverLog(LL_NOTICE,"REPLICAOF would result into synchronization "
"with the master we are already connected "
"with. No operation performed.");
addReplySds(c,sdsnew("+OK Already connected to specified "
"master\r\n"));
return;
}
sds client = catClientInfoString(sdsempty(),c);
serverLog(LL_NOTICE,"REPLICAOF %s:%d enabled (user request from '%s')",
miNew->masterhost, miNew->masterport, client);