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))) while ((ln = listNext(&li)))
{ {
redisMaster *miCheck = (redisMaster*)listNodeValue(ln); 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 // Pre-req satisfied, lets continue
@ -2380,15 +2381,10 @@ void replicaofCommand(client *c) {
if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != C_OK)) if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != C_OK))
return; return;
/* Check if we are already attached to the specified slave */ redisMaster *miNew = replicationAddMaster((char*)ptrFromObj(c->argv[1]), port);
listIter li; if (miNew == nullptr)
listNode *ln;
listRewind(g_pserver->masters, &li);
while ((ln = listNext(&li)))
{ {
redisMaster *mi = (redisMaster*)listNodeValue(ln); // We have a duplicate
if (!strcasecmp(mi->masterhost,(const char*)ptrFromObj(c->argv[1]))
&& mi->masterport == port) {
serverLog(LL_NOTICE,"REPLICAOF would result into synchronization " serverLog(LL_NOTICE,"REPLICAOF would result into synchronization "
"with the master we are already connected " "with the master we are already connected "
"with. No operation performed."); "with. No operation performed.");
@ -2396,10 +2392,7 @@ void replicaofCommand(client *c) {
"master\r\n")); "master\r\n"));
return; 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);
sds client = catClientInfoString(sdsempty(),c); sds client = catClientInfoString(sdsempty(),c);
serverLog(LL_NOTICE,"REPLICAOF %s:%d enabled (user request from '%s')", serverLog(LL_NOTICE,"REPLICAOF %s:%d enabled (user request from '%s')",
miNew->masterhost, miNew->masterport, client); miNew->masterhost, miNew->masterport, client);