Describe the bug

In Jedis's local regression test, the ip address was specified as 127.0.0.1, but because Redis was started according to ipv6, the test failed because of ::1 != 127.0.0.1.

Redis [BUG] The problem of using strcmp to compare localhost ip

Redis Code : Redis [BUG] The problem of using strcmp to compare localhost ip

Expected behavior

test should be passed.

Possible repair options

We may need to customize the ip comparison function to treat 127.0.0.1 and ::1 equally.

The sample repair code is as follows. The function naming and location may need to be adjusted. In addition, I suggest that we can use ipCmp to replace all strcmp or strcasecmp used for ip comparison in the Redis code.

diff --git a/src/replication.c b/src/replication.c
index 0f2512fcb..26eea14b5 100644
--- a/src/replication.c
+++ b/src/replication.c
@@ -3755,6 +3755,19 @@ void replicationStartPendingFork(void) {
     }
 }

+int ipIsLocalHost(char *ip) {
+    return !strcmp(ip, "127.0.0.1") ||
+           !strcmp(ip, "localhost") ||
+           !strcmp(ip, "::1");
+}
+
+int ipCmp(char *ip1, char *ip2) {
+   if (ipIsLocalHost(ip1) && ipIsLocalHost(ip2)) {
+       return 0;
+   }
+   return strcasecmp(ip1, ip2);
+}
+
 /* Find replica at IP:PORT from replica list */
 static client *findReplica(char *host, int port) {
     listIter li;
@@ -3772,7 +3785,7 @@ static client *findReplica(char *host, int port) {
             replicaip = ip;
         }

-        if (!strcasecmp(host, replicaip) &&
+        if (!ipCmp(host, replicaip) &&
                 (port == replica->slave_listening_port))
             return replica;
     }

Additional information

Redis Version: unstable branch Jedis Version: master branch

Comment From: yossigo

@yangbodong22011 Did you track down a specific change in Redis that broke this regression test?

Comment From: yangbodong22011

@yangbodong22011 Did you track down a specific change in Redis that broke this regression test?

No, you reminded me, I will try to find.

EDIT: When Jedis introduced this test, it should have failed on macOS, but Jedis's test environment did not include macOs until I encountered this error in my macOS laptop test.

@yossigo It doesn't seem to be the start of a particular PR. On my machine, with version 4.0 of Redis, info replication will also display the following information:

Redis [BUG] The problem of using strcmp to compare localhost ip

➜  redis git:(unstable) ✗ cat /tmp/redis-master.conf 
daemonize yes
protected-mode no
port 6388
pidfile /tmp/redis10.pid
logfile /tmp/redis10.log
save ""
appendonly no
➜  redis git:(unstable) ✗ cat /tmp/redis-slave.conf 
daemonize yes
protected-mode no
port 6389
pidfile /tmp/redis11.pid
logfile /tmp/redis11.log
save ""
appendonly no
slaveof localhost 6388
➜  redis git:(unstable) ✗